Style for certain word?

For discussion and questions related to CMS Specific templates and stylesheets (CSS), and themes. or layout issues. This is not a place for generic "I don't know CSS issues"
Post Reply
Elli
Forum Members
Forum Members
Posts: 25
Joined: Thu Mar 27, 2008 3:32 pm

Style for certain word?

Post by Elli »

Didn't know the words to search this topic so I have to start new one...

Okay, I have this problem with styles... I have certain products that are branded by color styles like this:
Elara mini
Metis Watti

Now I have a product catalog where I have these names in the menu and the content title. Is there any way to get this style assigned to the names?

I'm no good at java script but is it possible to make an If-Then- clause to make _every_ product name to change appearence when it appears in the menu/title/content? Or can I use some smarty tag in the title and menu fields to assign the colors?
sn3p
Forum Members
Forum Members
Posts: 169
Joined: Mon Oct 15, 2007 2:27 pm

Re: Style for certain word?

Post by sn3p »

You could use an UDT to do this.

Make a new UDT called "highlight_words" with the following code:

Code: Select all

if (!isset($params['content'])) {
	return;
}
$content = $params['content'];

/* an array of words to highlight and their replacements */
$words = array(
'Elara' => '<span class="highlight-black">El</span><span class="highlight-orange">ara</span>',
'Metis' => '<span class="highlight-black">Me</span><span class="highlight-orange">tis</span>'
);

foreach ($words as $word => $replacement) {
	$word = preg_quote($word);
	$content = preg_replace("/\b($word)\b/i", $replacement, $content);
}
return $content;
Fill the $words array with words that need highlighting, and their replacement.

Add this to your CSS:

Code: Select all

.highlight-black{
        color: #000;
        font-weight: bold;
}
.highlight-orange{
        color: #ffa500;
        font-weight: bold;
}
Now edit your template, and find where your content is included. Replace {content} with this:

Code: Select all

{capture name=content}{content}{/capture}
{highlight_words content=$smarty.capture.content}
This will capture your content and push it in the UDT.

I'm not sure if this is the best way to do this but it works. I think it will work for other modules aswell, but have'nt tested it.
Elli
Forum Members
Forum Members
Posts: 25
Joined: Thu Mar 27, 2008 3:32 pm

Re: Style for certain word?

Post by Elli »

Thank you very much! I'll have to try this, I'l get back if I screw somethin up ;)
viebig

Re: Style for certain word?

Post by viebig »

I would prefer to do it with JS, but, remember that, our helpful firend describe it, only for the content. in menu you will need to do:

Code: Select all

{capture name=newmenu}{menu}{/capture}
{highlight_words content=$newmenu}
sn3p
Forum Members
Forum Members
Posts: 169
Joined: Mon Oct 15, 2007 2:27 pm

Re: Style for certain word?

Post by sn3p »

Indeed, forgot to mention how to aply it to the menu ;)

Personaly I would avoid using JS if it can be done serverside. Any reason why you prefer JS?
viebig

Re: Style for certain word?

Post by viebig »

sn3p wrote: Indeed, forgot to mention how to aply it to the menu ;)

Personaly I would avoid using JS if it can be done serverside. Any reason why you prefer JS?
Imagine a large content section, with 2000+ hits per hour, being processed. Let´s say that if you have the best servers, and no worry abaout memory and performance, i would to it server side.  :)
Elli
Forum Members
Forum Members
Posts: 25
Joined: Thu Mar 27, 2008 3:32 pm

Re: Style for certain word?

Post by Elli »

Hi!

I managed to get this to work in the content area, but if I try to do the same for menu it wont work. i have menu tag like this

Code: Select all

{menu template='simple_navigation.tpl' start_level='2' collapse='1'} 
And if I use the given code for menu items like below, and some variations that I tried, it wont show anything

Code: Select all

{capture name=newmenu}{menu}{/capture}
{highlight_words content=$newmenu}
What did I miss? I tried to add another UDT with the name highlight_menuwords, tried to change the names in different placec and so on but no luck.
sn3p
Forum Members
Forum Members
Posts: 169
Joined: Mon Oct 15, 2007 2:27 pm

Re: Style for certain word?

Post by sn3p »

Does the code do nothing? No ouput or errors?

Try this:

Code: Select all

{capture name=newmenu}{menu template='simple_navigation.tpl' start_level='2' collapse='1'}{/capture}
{highlight_words content=$newmenu}
Elli
Forum Members
Forum Members
Posts: 25
Joined: Thu Mar 27, 2008 3:32 pm

Re: Style for certain word?

Post by Elli »

It does nothing, no menu at all. And no error. Don't have a clue what could be wrong... even the source code for the menu list is empty.
Last edited by Elli on Wed Sep 24, 2008 3:09 pm, edited 1 time in total.
sn3p
Forum Members
Forum Members
Posts: 169
Joined: Mon Oct 15, 2007 2:27 pm

Re: Style for certain word?

Post by sn3p »

My mistake, try this:

Code: Select all

{capture name=newmenu}{menu template='simple_navigation.tpl' start_level='2' collapse='1'}{/capture}
{highlight_words content=$smarty.capture.newmenu}
There is only one problem with the UDT, it will replace all instances of the words.
So it will also replace the words in urls, images and other html code.
To fix that update the 'highlight_words' UDT:

Code: Select all

if (!isset($params['content']))
	return;
$content = $params['content'];

$words = array(
	'Elara' => '<span class="highlight-black">El</span><span class="highlight-orange">ara</span>',
	'Metis' => '<span class="highlight-black">Me</span><span class="highlight-orange">tis</span>'
);

$parts = preg_split("/(<.*?>)/", $content, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($words as $word => $replacement){
	foreach($parts as &$part)
		if ($part[0] != '<')
			$part = str_replace($word, $replacement, $part);
	$content = implode('', $parts);	
}

echo $content;
Elli
Forum Members
Forum Members
Posts: 25
Joined: Thu Mar 27, 2008 3:32 pm

Re: Style for certain word?

Post by Elli »

Hi, thank you for help!

The first code works but the link correction code for the 'highlight_words' UDT doesn't. It wont let me save the UDT because of the error in code.
Post Reply

Return to “Layout and Design (CSS & HTML)”