Welcome Guest, Not a member yet? Register   Sign In
Template Parser Class Problem
#1

[eluser]Deep Arora[/eluser]
Is there a way to use conditionals (if else) in the template parser?

For example, I want to check if no records are returned in the resultset, then don't process the loop:

Code:
{resultset}
{title}
{/resultset}

is there a way to use "if" condition here?

Code:
{if resultset} ?
#2

[eluser]rogierb[/eluser]
No, you can't.

Well... not quite true, you can if you load the parsed template in a variable and eval() it.
Then you can use PHP inside you template.

Code:
if ("{some_element}" !="") echo "blahblah";
#3

[eluser]Phil Sturgeon[/eluser]
The Template Parser class is very limited. For better support, why not try working with Dwoo in your views?
#4

[eluser]Deep Arora[/eluser]
Thanks..I checked that library:

As an example it just shows:

Code:
$this->load->library('parser');
$this->parser->parse('dwoo_test', array('message' => 'OH HAI!'));
So, in view can we do like this?

Code:
{if dwoo_test} {message} {/dwoo_test}

The problem I am facing is that a resultset is returned to the view. Now in view, I use
Code:
{resultset}
{field}
{/resultset}

If nothing is returned from the db, then this just displays the tags. Even if I populate the resultset as "No records found", it just replaces the first {resultset} tag. Rest of the things is shown as tags.

In expression engine, I remember, we have something similar and we can use all conditionals too:

Code:
{weblog: name} etc

Can same thing be achieved here?
#5

[eluser]Deep Arora[/eluser]
[quote author="Phil Sturgeon" date="1264527544"]The Template Parser class is very limited. For better support, why not try working with Dwoo in your views?[/quote]

Does this work with http://igniteyourcode.com/ocular ? I am using this template library..
#6

[eluser]Phil Sturgeon[/eluser]
Yes, that syntax is wrong but as pseudo-code it is correct.

Ocular Template looks like it uses $this->load->view(), not parser. You be able to return the template string, then parse it but I do not know enough about Ocular library for that.

It's fully compatible with my Template library though. ;-)
#7

[eluser]moodh[/eluser]
You can trick the parser with the following code:
Controller:
Code:
if(logge_in()) {
    $data['if_logged_in'] = array(array());
} else {
    $data['if_logge_in'] = array();
}
And in the view:
{if_logged_in}Welcome!{/if_logged_in}

An empty array hides it, and an array containing one row (array(), which could be like.. 'hi' as well) will show it.




Theme © iAndrew 2016 - Forum software by © MyBB