Adam Posted December 9, 2008 Share Posted December 9, 2008 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 Quote Link to comment Share on other sites More sharing options...
Adam Posted December 9, 2008 Author Share Posted December 9, 2008 BUMP! Quote Link to comment Share on other sites More sharing options...
premiso Posted December 9, 2008 Share Posted December 9, 2008 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? Quote Link to comment Share on other sites More sharing options...
Adam Posted December 10, 2008 Author Share Posted December 10, 2008 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 Quote Link to comment Share on other sites More sharing options...
Adam Posted December 10, 2008 Author Share Posted December 10, 2008 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 Quote Link to comment Share on other sites More sharing options...
blueman378 Posted December 10, 2008 Share Posted December 10, 2008 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. Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 10, 2008 Share Posted December 10, 2008 You could use regex_replace Quote Link to comment Share on other sites More sharing options...
Adam Posted December 10, 2008 Author Share Posted December 10, 2008 Hey. Cheers Mchl, used replace instead of regex_replace (believe it's faster?) but got it working! Thanks a lot! A Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 10, 2008 Share Posted December 10, 2008 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} Quote Link to comment Share on other sites More sharing options...
Adam Posted December 11, 2008 Author Share Posted December 11, 2008 Ahh thanks rhodsea! Because the menus would always be the same for each time I used sprintf, could I just use: {$menuHtml|sprintf:1} ?? Thanks for the reply! A Quote Link to comment Share on other sites More sharing options...
Adam Posted December 11, 2008 Author Share Posted December 11, 2008 After a quick test I found you couldn't - not like that anyway! Do you know of a way? Cheers! A Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 11, 2008 Share Posted December 11, 2008 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} Quote Link to comment Share on other sites More sharing options...
Adam Posted December 12, 2008 Author Share Posted December 12, 2008 No joy :/ Cheers for helping me though! A Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.