Welcome Guest, Not a member yet? Register   Sign In
how to place some php decision code insite a template iteration
#1

[eluser]dvogel[/eluser]
I'm using the template parser for building my page from a template. The view is the real page content. I'm reading weblog items and placing them in the page using the following code

Code:
<div id="weblog">
    {weblog_records}
        <div class="entry">
            <div class="header">
                <div><a href="?/weblog/index/{id}">{title}</a></div>
                <div>door {name} {date}@{time}</div>
            </div>
            <div class="body">{message}</div>
        </div>
    {/weblog_records}
</div>

But i want the messages to be truncated. This will be done in the controller. The array containing the title, name, date, time and message gets a new variable messageTruncated which is true when the message was to long and was truncated. Within the {weblog_records}...{/weblog_records} iteration I want to check this boolean so I can place a html link to the complete message like this code:

Code:
&lt;?php
if ($messageTruncated == true)
{
    echo '<a href="?/weblog/index/{id}">read more...</a>''    
}
?&gt;

How can i access the variables within the current entry of the {weblog_records}...{/weblog_records} iteration?
#2

[eluser]Pascal Kriete[/eluser]
I can't test this, but the parser should run before the php in your view is processed. So instead of adding a variable if it is truncated, always include a $truncated variable - just make it default to false.

Now this:
Code:
<div id="weblog">
    {weblog_records}
        <div class="entry">
            <div class="header">
                <div><a href="?/weblog/index/{id}">{title}</a></div>
                <div>door {name} {date}@{time}</div>
            </div>
            <div class="body">{message}</div>
        </div>
        &lt;? if ({truncated} == true}) echo "whatever link that was"; ?&gt;
    {/weblog_records}
</div>

Will hopefully parse to yield if(true == true) or (false == true), which would do what you want it to do.
#3

[eluser]dvogel[/eluser]
[quote author="inparo" date="1200506597"] &lt;? if ({truncated} == true}) echo "whatever link that was"; ?&gt;
[/quote]
It didnt work.. getting a syntax error. And using if ('{truncated}' == true}) is just check it as a string not the contact of the variable. So bad luck for me I think.

Anyone else?
#4

[eluser]jnickerson[/eluser]
Couldn't you do this in the controller? Before you post to the $data array just determine if the truncating should happen and then the view won't need any php conditionals. This would probably be the preferred method since you are separating the logic from the view.
#5

[eluser]dvogel[/eluser]
[quote author="jnickerson" date="1200529877"]Couldn't you do this in the controller? Before you post to the $data array just determine if the truncating should happen and then the view won't need any php conditionals. This would probably be the preferred method since you are separating the logic from the view.[/quote]
Yes I know but the 'read more' link should not be after the text but at the right. I could build the html in the controller and just put the html in the view using the template parser.

I still find I easier to do in the view, its not realy business logic but more presentation logic. But when its not possible, I have no option
#6

[eluser]Michael Ekoka[/eluser]
Hey dvoge, why would you wanna use the parser class if you already understand PHP and you're the one doing the templating. What's wrong with foreach{}? Common man, make your life simpler, go back to the view library. Template engines à la Smarty are for people aiming at fixing something that was not broken. Unless you don't trust your designer or he finds php really confusing, go back to the roots. Yes, PHP was itself created to be... a Template Engine.

Like the documentation says :
Quote:CodeIgniter does not require you to use this class since using pure PHP in your view pages lets them run a little faster. However, some developers prefer to use a template engine if they work with designers who they feel would find some confusion working with PHP.
#7

[eluser]wiredesignz[/eluser]
I agree with CodeIgniter above, the parser library is very limited. raw PHP in templates is faster and more flexible.




Theme © iAndrew 2016 - Forum software by © MyBB