Jump to content

Smarty printf() like function..


Adam

Recommended Posts

Hey up. Fairly new to Smarty..

 

Have a problem with a menu generation template I'm working on. Basically as there's about 12 different languages supported on this site there's a file to generate the menus for each. Each site has about 7 menus, some with additional menus coming of those. I decided to neaten the code up and to decrease the repetitive code.

 

What I'm trying to do is create a variable that will be accessible across all the files called $menuHtml, then perform sprintf() like function on it as the variable is called. So something like:

 

{assign var='menuHtml' value=' style="padding-bottom: 7px;" onmouseover="popUp(\'HM_Menu%s\', event);" onmouseout="popDown(\'HM_Menu%s\');"'}

 

(notice the 2 %s')

 

then try and call it with something like:

 

{$menuHtml|1}
{$menuHtml|2}
{$menuHtml|2_2}
{$menuHtml|3}

(...)

 

To produce something like:

 

style="padding-bottom: 7px;" onmouseover="popUp('HM_Menu1_1', event);" onmouseout="popDown('HM_Menu1_1');"

 

if that makes sense? I just can't find anything to help me!

 

If anybody could help or give me a nudge in the right direction I'd be really grateful!

 

Cheers! Adam

Link to comment
https://forums.phpfreaks.com/topic/136197-smarty-printf-like-function/
Share on other sites

Ok, here is a question.

 

Why not assign menuHtml in the page that includes the smarty?

 

Another question...why not have a language variable which would include the right menu?

 

Another question...why not use the {include} and include a menu.php file where the language is set in session so it includes the correct menu?

Hah fair questions. That was a route I was hoping to avoid with the complexity of the framework I'm working with. The way I mentioned (in my case) is the most efficient and easiest, if I could implement it!

 

The system already includes many, many files! so I'm trying to avoid extra files being included; a quick extract from the log created on a single page load:

 

[WWWPageIEU] 40896 included lines in 127 files (excluding skins)

 

I suppose I could assign the menuHtml var to the PHP file generating the template, but I would still like to have the same functionality, so like:

 

$this->template->assign('menuHtml', ' style="padding-bottom: 7px;" onmouseover="popUp(\'HM_Menu%s\', event);" onmouseout="popDown(\'HM_Menu%s\');"');

 

However, I still don't quite know how I could then call $menuHtml with dynamic values?

 

Any help? Cheers for your response..

 

Adam

Also forgot to mention the file is called: "menu_configuration.tpl" .. When the framework includes the file, it automatically calls the correct site code's version, so for the UK it's: "menu_configuration.IUK.tpl", French: "menu_configuration.IFR.tpl" .. Which rules out any conditional includes, or mixing the files.. What I want is just a kind of template variable that two small parts of it can be dynamically changed when I call the var - trying to avoid any extra includes where possible!

 

A

Hi there.

 

you could always create a custom smarty function, they are basically formatted like a normall php function (just different naming conventions)

 

all php is available through the function.

 

if your not sure how to do this let me know.

p.s. - your original post was very close. you would use sprintf as the modifier though and it would look like:

{assign var='menuHtml' value=' style="padding-bottom: 7px;" onmouseover="popUp(\'HM_Menu%s\', event);" onmouseout="popDown(\'HM_Menu%s\');"'}
{$menuHtml|sprintf:1:1}
{$menuHtml|sprintf:2:2}
{$menuHtml|sprintf:2_2:2_2}
{$menuHtml|sprintf:3:3}

try:

{assign var='menuHtml' value=' style="padding-bottom: 7px;" onmouseover="popUp(\'HM_Menu%1%s\', event);" onmouseout="popDown(\'HM_Menu%1%s\');"'}
{$menuHtml|sprintf:1}
{$menuHtml|sprintf:2}
{$menuHtml|sprintf:2_2}
{$menuHtml|sprintf:3}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.