Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check a complex condition in Smarty(PHP)

I need to display a section or another in a smarty template. My condition is simple: if a smarty value starts with a string I should display one section, otherwise the other smarty section should be displayed. I can change only the tpl files.

    {php}
    if (substr($url,0,4) != 'http')
    {
    {/php}
                  section 1

    {php}
    }
    else
    {
    {/php}
        section 2   
    {php}
    }
    {/php}

The problem is that I can not read the url varible which was previously assigned using $smarty->assign. Basically, I'm looking for the smarty function that can be used to retrieve a value, or if there is a better solution.

like image 267
adiian Avatar asked Jun 03 '10 19:06

adiian


People also ask

How does PHP Smarty work?

PHP syntax works well for application code, but quickly degenerates when mixed with HTML. Smarty's simple {tag} syntax is designed specifically to express presentation. Smarty focuses your templates on presentation and less on "code". This lends to quicker template deployment and easier maintenance.


1 Answers

First, I would clean up your code. You don't need php tags, you're using smarty:

 {if substr($url,0,4) neq 'http'}

     section 1

 {else}
        section 2   
 {/if}

That's untested but it should be pretty close..

Now, if you're trying to read something like a constant, for example a server variable like HTTP_HOST, you can do something like this:

 {assign var='url' value=$smarty.server.HTTP_HOST}

 {if substr($url,0,4) neq 'http'}

     section 1

 {else}
        section 2   
 {/if}
like image 158
pws5068 Avatar answered Oct 18 '22 03:10

pws5068