Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Templating system #38

Open
xtrasmal opened this issue Nov 10, 2012 · 5 comments
Open

Templating system #38

xtrasmal opened this issue Nov 10, 2012 · 5 comments

Comments

@xtrasmal
Copy link

Right now I want to show the latest posts, the latest comments and the latest projects on the DefaultHome.tpl.php

I do this by creating a copy of the PostListView, CommentsListView, ProjectsListView and
do:

$this->display('HomePostsListView.tpl.php');

Is this the way to go or is there a nicer way I can render loads of different stuff on one page?

@jasonhinkle
Copy link
Owner

Well, there's a few ways you could go about that. One - if you are feeling adventurous on your home page is to dig into backbone and do some model binding for those recent posts via ajax. If you are not really concerned with it being fancy like that, you can just do it the old fashioned way, for example:

Inside DefaultController->Home you need to do a database read and then assign the results to the template like so:

$criteria = new PostCriteria();
$criteria->SetOrder('CreatedDate', true);  // <- sort descending by created day

$posts = $this->Phreezer->Query('Post',$criteria)->GetDataPage(1,5)->ToObjectArray(); // <- get only 5 records

$this->Assign('posts',$posts);

Then inside ViewDefaultHome.tpl you would loop through them to display. That code will be slightly different depending on whether you're using Smarty or Savant. Here would be the smarty code:

{foreach from=$posts item=post}
<div>{$post->CreatedDate|escape} by {$post->Author|escape} : {$post->Title|escape}</div>
{/foreach}

Savant is just regular PHP code and would look something like this:

<? foreach ($posts as $post) {
    echo '<div>' ;
    $this->eprint($post->CreatedDate . ' by ' . $post->Author . ' : ' . $post->Title);  // using eprint to prevent css exploits
    echo '</div>';
}?>

@xtrasmal
Copy link
Author

I will dive into the adventure. I want to take full advantage of Phreeze, so because its Javascript MVC on top of Savant MVC, I need to stick with models.

@jasonhinkle
Copy link
Owner

cool. It's mainly going to be javascript/backbone code in that case. The Phreeze REST API is already set up and ready for you to read the data.

Learning backbone/underscore takes a bit of time and you definitely will want to get familiar with their support documentation. It took me about a week to really get comfortable with it. But once you figure it out it will be very liberating and you'll probably have a greater understanding of what Phreeze really is, and how the generated code is in many cases just a suggestion of how you should build things.

I feel like the Javascript that Phreeze generates is pretty decent but not 100% perfect and I'm waiting for somebody to come along with a deep understanding of backbone to help clean it up!

@xtrasmal
Copy link
Author

There are alternatives to Phreeze, like the yii framework. I haven't tested it out fully, but after 12 hours I really got frustrated with it. Phreeze feels cleaner, intuitive, easier to setup and smarter, but, more important Phreeze implements backbone. This is a real big plus. Just imagine doing this afterwards :p It would take me ages.
So I'll start working on the documentation and see how backbone works and such, now that I have the authentication
in place.

@xtrasmal
Copy link
Author

For everybody who is reading this:

                   <?php foreach ($this->posts as $post => $val): ?>
                    <?php echo $this->eprint($val->Title); ?><br/>
                    <?php endforeach; ?>  

Would get you the parsed data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants