php code in template

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
pikkp

php code in template

Post by pikkp »

Is it somehow possible to insert php code to template between the '{' '}'?
Example:
{cms_module module="menumanager" collapse="0" start_element=" "}
I know that this variant doesn`t work, but is there some way to do similarly?
jelle

Re: php code in template

Post by jelle »

Create a custom tag. You can enter your php snippet there, and call the {tag} in your template.
priit

Re: php code in template

Post by priit »

I found the solution!

I created object and afterwards used the object in content

I must remark that i can`t use tag because using { inside { doesn´t work!

Thanks anyway
jelle

Re: php code in template

Post by jelle »

you are not supposed to use a {tag} inside another tag, or use {} within them. If you really need to do that, most likely you are pushing the boundaries of smarty, so you'd better write your stuff in php.
Dan

php code in template

Post by Dan »

Not sure if this is the correct place in the forum, but I'd like to share this with newbie's (like myself) to cmsmadesimple. I placed the below code into a user defined tag, and then referenced it in my template {the_name_of_my_userdefinedtag}. what it does is take the "name" of a page and determines what HTML gets writen in order to control images, nav on states, etc. once you have the name of the page, the if, elseif and else statement can control enything in the template. it saved me from having 10 versions of the nav (I needed an on state when the user was on a particular page, and to insert some javascript on the homepage only). Being able to do this saved me a ton of time.
-----------------------------------------------------------

global $gCms;
$thispage = '';
$thispage = $gCms->variables['page_name'];

if ($thispage == the_name_of_one_of_your_pages)  echo "something_you'd_like_written_to_this_page";

elseif  ($thispage == the_name_of_another_one_of_your_pages) echo "something_else_you'd_like_written_to_this_page";

else echo "something_you'd_like_written_as_your_default";
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: php code in template

Post by calguy1000 »

Very cool Dan, thanks!!

I think it provides us with a perfect example of how to use user defined tags.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
jelle

Re: php code in template

Post by jelle »

Yes, that reminds me, I should try to give more encouragement en compliments. If only I knew how to do that easy...
Here's one try: thanks Calguy for reminding me.
Here's another: Thanks Dan, that is usefull code. Maybe one could use that too to support icons in the naviagtion?

(hmm... no... probably not like that. But volunteers are welcome to show how they used a customtag(or smarty?) to support pretty pictures in their menu. 
User avatar
Elijah Lofgren
Power Poster
Power Poster
Posts: 811
Joined: Mon Apr 24, 2006 1:01 am

Re: php code in template

Post by Elijah Lofgren »

jelle wrote: Maybe one could use that too to support icons in the naviagtion?

(hmm... no... probably not like that. But volunteers are welcome to show how they used a customtag(or smarty?) to support pretty pictures in their menu. 
Here's my custom menu template that I use in the Menu Manager. It uses the Silk Icons

Code: Select all

{if $count > 0}
<ul class="menu_horiz">
{foreach from=$nodelist item=node}
{if $node->depth > $node->prevdepth}
{repeat string="<ul>" times=$node->depth-$node->prevdepth}
{elseif $node->depth < $node->prevdepth}

{repeat string="</li></ul>" times=$node->prevdepth-$node->depth}
</li>
{elseif $node->index > 0}</li>
{/if}

{if $node->index == 0}
{assign var="icon" value="house"}
{elseif $node->haschildren == true }
{assign var="icon" value="folder"}
{elseif $node->haschildren == false }
{assign var="icon" value="page"}
{/if}

{if $node->current == true}
<li style="list-style-image: url('/images/icons/{$icon}_go.png') ">{$node->menutext}
{else}
<li style="list-style-image: url('/images/icons/{$icon}.png') "><a href="{$node->url}"{if $node->target ne ""} target="{$node->target}"{/if}>{$node->menutext}</a>
{/if}
{/foreach}

{repeat string="</li></ul>" times=$node->depth-2}</li>
</ul>
{/if}
See attached picture for an example of what it looks like.

[attachment deleted by admin]
Note: I don't have time to take on any more projects. I'm quite busy. I may be too busy to reply to emails or messages. Thanks for your understanding. :)
pikkp

Re: php code in template

Post by pikkp »

If we follow example above, then i have another question.
I enter UDT name into template and the source of tag is down below.
My web page prints out:
{cms_module module=FrontEndUsers login='true' lang='et_EE' }

How can i do that i see login box instead of a tag?

tag source:
global $gCms;
$thispage = '';
$thispage = $gCms->variables['hl'];

if ($thispage == 'ee')  echo "{cms_module module=FrontEndUsers login='true' lang='et_EE' }";

elseif  ($thispage == 'en') echo "{cms_module module=FrontEndUsers login='true' lang='en_US'}";

else echo "{cms_module module=FrontEndUsers login='true' lang='et_EE'}";

Thanks in advance
Post Reply

Return to “Tips and Tricks”