Style for certain word?
Style for certain word?
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?
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?
Re: Style for certain word?
You could use an UDT to do this.
Make a new UDT called "highlight_words" with the following code:
Fill the $words array with words that need highlighting, and their replacement.
Add this to your CSS:
Now edit your template, and find where your content is included. Replace {content} with this:
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.
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;
Add this to your CSS:
Code: Select all
.highlight-black{
color: #000;
font-weight: bold;
}
.highlight-orange{
color: #ffa500;
font-weight: bold;
}
Code: Select all
{capture name=content}{content}{/capture}
{highlight_words content=$smarty.capture.content}
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.
Re: Style for certain word?
Thank you very much! I'll have to try this, I'l get back if I screw somethin up 

Re: Style for certain word?
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}
Re: Style for certain word?
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?

Personaly I would avoid using JS if it can be done serverside. Any reason why you prefer JS?
Re: Style for certain word?
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.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?

Re: Style for certain word?
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
And if I use the given code for menu items like below, and some variations that I tried, it wont show anything
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.
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'}
Code: Select all
{capture name=newmenu}{menu}{/capture}
{highlight_words content=$newmenu}
Re: Style for certain word?
Does the code do nothing? No ouput or errors?
Try this:
Try this:
Code: Select all
{capture name=newmenu}{menu template='simple_navigation.tpl' start_level='2' collapse='1'}{/capture}
{highlight_words content=$newmenu}
Re: Style for certain word?
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.
Re: Style for certain word?
My mistake, try this:
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
{capture name=newmenu}{menu template='simple_navigation.tpl' start_level='2' collapse='1'}{/capture}
{highlight_words content=$smarty.capture.newmenu}
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;
Re: Style for certain word?
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.
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.