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

[eluser]coldKingdom[/eluser]
Hi Guys!

I'm using CI's template parser for my news page at the moment and i'm wondering about one thing. I need to use the date function like this

Code:
<?= date("M", strftime('{date}')); ?>

where {date} has the value 1230415583 from the database, but it's not really working for me Smile

is there a way to make this work, or do I need to print it out like an array?

Many thanks!

/Andreas
#2

[eluser]thinkigniter[/eluser]
Try this

In your controller do...
Code:
$my_array = array();
$my_array[ 'month'=> date("M", 1230415583 )) ];
$this->load-view('my_view',$my_array);

In your view do...
Code:
Todays month is:
<?php  echo $month;?>
#3

[eluser]coldKingdom[/eluser]
Thank you for your answer!

But I don't think I was to clear in my first post, and I'm sorry about that. The thing is that I want to show 3 news on my start page and all has different dates from my MySQL database.

My code looks like this right now
Code:
<div class="span-13 colborder">
    {news}
    <div class="span-13 last news">
        <h3>
            <span class="date">
                <span class="month">&lt;?= date("M", strftime('{date}')); ?&gt;</span>
                <span class="day">&lt;?= date("j", strftime('{date}')); ?&gt;</span>
            </span>
            {header}
        </h3>
        <p>
            {text}
        </p>
    </div>
    {/news}
</div>

The dates doesn't really work as it is how I want to achieve this Smile

/Andreas
#4

[eluser]Phil Sturgeon[/eluser]
Right now you are saying that the date you are using is literally {date}, as CI's template parser has no idea what is going on between those PHP tags.

You will either need to format that date in the controller, or run the entire loop in pure PHP.

The template parser seems like a great idea, but it is still very lacking and sadly may well be the weakest library in CI. I used it, then binned it after the first project as it is buggy and inflexible.

Try Smarty? Yet another Smarty Thread
#5

[eluser]simonmaddox[/eluser]
[quote author="pyromaniac" date="1230566223"]
The template parser seems like a great idea, but it is still very lacking and sadly may well be the weakest library in CI. I used it, then binned it after the first project as it is buggy and inflexible.
[/quote]

I'm currently rewriting our site using the Template Parser class, and I have to disagree with you. The whole point of the view template is that there should be no PHP at all in it - everything should be set in the controller.

So, you should be parsing your date in the controller, not in the view. This gives you a lot more flexibility in the future, and means that your views are much more reusable.

I'm perfectly happy with how the template parser works, and the fact that none of our PHP goes into it means that our designer can quickly and easily build his layout.
#6

[eluser]Phil Sturgeon[/eluser]
I'd say the main thing is that the view should have as little logic as possible, doesn't matter which kind of tags show up.

If you are worried about designers, most editors will recognise the PHP tags and show a symbol or hide them on the Design mode, whereas random tags will show up as broken or plain text. That's no help for the designer at all!

Also, the ability to make minor changes to a value without having to set X different versions of the same variable are a godsend. What's the point in returning and array/object from your model, then having to loop through every item simply to set a few different date formats?

I think the one thing - other than the obvious bugs - missing from the Template Parser is Smarty-like modifiers. If you were using Smarty here, you could simply do:

Code:
<span class="day">{date|date_format:"%e"}</span>

Disclaimer: my point about Template Parser being buggy is based it's state at the time. It had trouble using the same loop twice on one page and loops had trouble with non-zero based indexes. It may be better by now, but it is still lacking.




Theme © iAndrew 2016 - Forum software by © MyBB