Page 1 of 1

Using {title} variable in function?

Posted: Mon Jul 09, 2007 9:22 am
by Tealon
I have the following code to get a specific header image on my website using the {title} variable. Unfortunately the code does not work right. The $title variable is not replaces like i am used to with the smarty engine. Can someone help me on how to implement the variable right?


      {if file_exists("images/cms/$title.gif")}
         
      {else}
         
      {/if}

Re: Using {title} variable in function?

Posted: Mon Jul 09, 2007 11:48 am
by SimonSchaufi

Re: Using {title} variable in function?

Posted: Mon Jul 09, 2007 11:58 am
by Tealon
This is not what I'm asking. If look at my code, you see that the problem is the if_exists function. I want to use the {title} variable in the function, because {title} can not be nested in a smarty tag and $title does not work, because its somehow not related to {title}.

and b.t.w. i have searched, and red all the topics you gave me.

Re: Using {title} variable in function?

Posted: Mon Jul 09, 2007 12:17 pm
by cyberman
Have you tried to capture title tag?

http://smarty.php.net/manual/en/languag ... on.capture

Re: Using {title} variable in function?

Posted: Mon Jul 09, 2007 2:05 pm
by Tealon
I have tried this code, but it also does not work:

{capture name=pakken assign=titlecap}
images/cms/{title}.gif
{/capture}


 
{if file_exists($titlecap)}
 
{else}
  {$titlecap}
{/if}


Does someone know what I'm doing wrong?

Re: Using {title} variable in function?

Posted: Tue Jul 10, 2007 10:16 am
by cyberman
Tealon wrote:       {if file_exists("images/cms/$title.gif")}
Are you sure file_exists() is a Smarty function ;)? And you need a Smarty function ...

If you wanna use php inside Smarty you have to mask it with {php}{/php} and activate the use in config.php (use_smarty_php_tags).

Re: Using {title} variable in function?

Posted: Tue Jul 10, 2007 1:06 pm
by Tealon
Again is the $title variable not recognized and the file_exists function is therefore not working.

Maybe another way is possible. Where does the smarty module assigns the value to {title}? I think it is easier to do the file_exist check there and pass a variable on to the template with true or false.

@edit:
I worked on this problem for a few hours today, and tried a UDF. But again the function file_exists is not taking any variable, while a direct string to the file is no problem....

So can anyone tell me why it is working with the sting and not with the variable?

Btw:  i checkes the content of the variable a lot of times en its always oke.

Re: Using {title} variable in function?

Posted: Tue Jul 10, 2007 3:52 pm
by alby
Tealon wrote: I have tried this code, but it also does not work:

{capture name=pakken assign=titlecap}
images/cms/{title}.gif
{/capture}


 
{if file_exists($titlecap)}
 
{else}
  {$titlecap}
{/if}


Does someone know what I'm doing wrong?
And this?
{capture name=pakken assign=titlecap}images/cms/{title}.gif{/capture}


 
    {myudt img=$titlecap}
 
UDT myudt:
if (file_exists($params['img']))
  echo '';
else
  echo ''.$params['img'];
Alby

Re: Using {title} variable in function?

Posted: Tue Jul 10, 2007 4:06 pm
by Tealon
Same problem again. With $params['img'] in the file_exists function it does not work. When I echo the $params['img'] out and use the printed string in the file_exists, it does not work..... Looks like the file_exists function can not take up a variable somehow...

@edit:
While I was just trying stuff out i cam across something very strange. A var_dump of the variable gives in the source code this:

string(22) "images/cms/Home.gif
"

It looks like a enter in the string.... This is new for me. Has anyone encountered this before?

Re: Using {title} variable in function?

Posted: Tue Jul 10, 2007 4:19 pm
by alby
Tealon wrote: Same problem again. With $params['img'] in the file_exists function it does not work. When I echo the $params['img'] out and use the printed string in the file_exists, it does not work..... Looks like the file_exists function can not take up a variable somehow...

@edit:
While I was just trying stuff out i cam across something very strange. A var_dump of the variable gives in the source code this:

string(22) "images/cms/Home.gif
"

It looks like a enter in the string.... This is new for me. Has anyone encountered this before?
try with: {capture name=pakken assign=titlecap}images/cms/{title}.gif{/capture}

Alby

Re: Using {title} variable in function?

Posted: Tue Jul 10, 2007 7:13 pm
by Tealon
That wat it!

Thanx Alby!

Re: Using {title} variable in function?

Posted: Mon Jun 13, 2011 1:08 am
by blackhawk
I'm having a small problem with this technique - capture automatically escapes single quotes found in titles. so you can't do conditional statements the way you want. for example, put in

[don't]

as a page title

and then capture that title variable, you can't compare it to variable value [don't] from another array.

anyway around this???

Re: Using {title} variable in function?

Posted: Mon Jun 13, 2011 1:16 am
by blackhawk
With the title being [don't], This didn't work for me...

Code: Select all

{capture assign="mtitle"}{title}{/capture}
<p>
{foreach from=$mstats item=entry}
{if $entry.team == $mtitle}
Season Record: <b>{$entry.record}</b>
{/if}
{/foreach}
</p>

but this did...

Code: Select all

{strip}
<p>
{foreach from=$mstats item=entry}
{if ($entry.team == $content_obj->Name())}
Season Record: <b>{$entry.record}</b>
{/if}
{/foreach}
</p>
{/strip}
So I'm cool!