Welcome Guest, Not a member yet? Register   Sign In
Phil Sturgeon Template Library and jquery ajax
#1

[eluser]joeizang[/eluser]
hi,

Please bear with me on this. I have a two fold question: First, I have checked the user guide of the template library by phil (which I have registered in the past to be very good) and I cannot find or understand what the
Code:
$template['body']

is for. I have had to do several projects without ever using it but I am having an issue that I thought maybe I should be looking at it because I was looking at code from his pyrocms and saw this type tag {template:body} and wondered what it was and how do you use it properly.
This leads me to my second question: I have had no joy doing jquery ajax with this template library. How do you do it? sorry for how vague the second question is I am trying things but I thought I should ask.

We have a saying where I am from: "He that asks never looses his way..."

Thanks.
#2

[eluser]TomTom[/eluser]
joeizang sorry I can't help you with your second question, but I'll try to answer the first one.

I agree that documentation for this otherwise brilliant library is unfortunately not very helpful, at least for beginners like myself. This is how you use it:

Code:
// This goes in a controller's method

// Any data to prepend before metadata in the page's head
$this->template->prepend_metadata('some code');
// Any data to append after metadata in the page's head
$this->template->append_metadata('some other code');

// Set view partials which are repeated throughout pages
// (file paths are relative to application/views)
$this->template->set_partial('header', 'path/to/file1');
$this->template->set_partial('footer', 'path/to/file2');

// Inject some code in a view partial
$this->template->inject_partial('sidebar', '<p><a href="#">Some</a> code</p>');

// Set a theme folder (application/themes/themefolder)
$this->template->set_theme('themefolder');

// Set a layout name (application/themes/themefolder/views/layouts/layoutname.php).
// If a theme isn't set, defaults to application/views/layours/layoutname.php
$this->template->set_layout('layoutname');

// Set value of $template['title']
$this->template->title('Some title');

// Set a variable called $varname and pass a value to it
$this->template->set('varname', 'Value of the $varname variable');

// Load the layout file and pass variables and view partials to it.
// mainview.php is the actual content of that particular loaded page
$this->template->build('mainviewfile');
Now, you don't benefit much if you put all this code in every method. The best results are achieved if you create a MY_Controller which extends CI_Controller, and put some or all of this code in it. You can also put some or all of this code in a __construct method of your controller so that it is processed for every method in it. Basically, the only code mandatory to go in the method is the very last line. Try to experiment a bit until you get results best for you.

This is an example of layoutname.php whether it be in views/layouts/ or themes/themefolder/views/layouts:

Code:
// Load the 'header' partial defined in the controller
&lt;?=$template['partials']['header'];?&gt;

// Load the 'title' from controller
<h1>&lt;?php echo $template['title']; ?&gt;</h1>

// Load the mainviewfile.php which contains the actual page content
&lt;?php echo $template['body']; ?&gt;

// Load the 'sidebar' partial from controller
&lt;?=$template['partials']['sidebar'];?&gt;

// Load the 'footer' partial from controller
&lt;?=$template['partials']['footer'];?&gt;

If you set up your controllers right (and extend them properly), you can get web pages that load a Header, Page Title, Sidebar and Footer automatically, and only change the inner content, depending on which page (which controller method) is loaded. You can also override this in every specific method if needed, the same way you've set it all up.
#3

[eluser]joeizang[/eluser]
First, TomTom for taking the time to answer the question, THANK YOU. You have shed light quite nicely on this issue for me. If I understand you properly, does
Code:
$template['body']
contain the main body of the page say text or the main bulk of content of the page?

But all I can say is thanks for taking the time to answer
#4

[eluser]TomTom[/eluser]
No problem, it's a pleasure to be of help, just like I've had help from online community when I needed it Smile As I said, it is not easy for beginners to find their way through this library's documentation, so I hope this will be helpful for others too.

Yes, you are right. $template['body'] is where the "meat" of the page is. For example, on a single blog post's page, it will be the post content, on a blog archive page it will be the list of posts etc. In other words, it's the opposite of the content that repeats throughout most of the pages, like header, footer, sidebar etc.
#5

[eluser]moth[/eluser]
Useful post guys. It took me a while to get to grips with the library.

In case it helps people in the future, I was trying to find a way of using the prepend_metadata/apend_metadata data to set some JavaScript in the footer of my template. In a word, you can't. However, I found the Inject Partial method will suffice.

Contrary to the standard partial, inject_partial is like a dynamic reference... you don't need to create the partial as a partial file...;

Controller code;

Code:
$this->template->inject_partial('footer_meta', '&lt; script src="/js/bootstrap.min.js"&gt;&lt;/ script>');

In the template;

Code:
&lt;?php echo $template['partials']['footer_meta']; ?&gt;

Hope that saves someone going down a blind ally.




Theme © iAndrew 2016 - Forum software by © MyBB