Help - Search - Members - Calendar
Full Version: Using includes with MT?
Movable Type Community Forum > Additional Resources > Tips and Tricks
tdellaringa
Hi folks,

I want to include some of my own chunks of code in my sidebar without repeating them for every template. Such as navigation and such.

Let's say for example I have a small HTML file with this in it:

CODE
<ul>
<li>my nav</li>
</ul>

<p>some other stuff</p>


I want to include that in all my templates, but as an include. I know these are perl pages (well I think they are) - how do I do this?

Thanks

Tom
almuhajabah
You can do this inside of MT by creating a template module. Go to your templates page and scroll way down to the bottom. There's a link where you can create a new template module. Give your module a name and enter the code that you want, then save it.

Now go to the template(s) where you want the code to appear and find the exact place in the template. Insert the following:

CODE
<$MTInclude module="modulename"$>


where "modulename" is the name of the module that you created. Rebuild the template and the new code should show up smile.gif
tdellaringa
Awesome! Thank you again for the quick and helpful answer smile.gif
LisaJill
Just as an alternate suggestion, the modules can slow down some rebuild times and don't change dynamically (You'd have to rebuild the entire site to make a change in the module)

You can use normal includes on the pages. Most of them are not CGI per se (with the exception of the search and popups) and so if you want to use ssi you simplyo need to change the extension to shtml or php - or fix .htaccess to parse html for either of those, and use your preference of ssi.

The common preference is php, I'd say. =)
almuhajabah
Actually, I have seen a number of people who are specifically looking to optimize their rebuild times say that switching from a template module to an index template included by SSI or PHP does not actually improve performance that much.

MT includes are a quick and simple way to include chunks of code that will work on any system that has MT installed, whether or not the user has or knows how to use SSI or PHP.
LisaJill
My experience with that differs. I went nutsy with MT Includes when I first got it, and had several. After replacing what I could with PHP includes I shaved approximately 3 seconds off my rebuilds. That was most noticeable in my commenting times.

Plus, not having to rebuild so often, if I simply added a link or what not. Since every page uses all the includes, it has saved me a LOT of rebuilding.

Anway, that's just my experience. =) I agree MT Modules are the easiest, especially for newcomers!
almuhajabah
I suppose it depends on what you have in the includes and what pages you're including them on. I only have sidebars on one of my index pages rather than sitewide so it really doesn't save me any time whether the include is rebuilt as a template module when the index page is rebuild or whether it's rebuilt separately as an index page and then included. The same file is rebuilt once when a new entry is added or a new comment is posted, whichever method I use.

I just think it's incorrect to make a blanket statement that using PHP or SSI will always improve rebuild times.
LisaJill
Mmm, I never said will definitely speed up rebuilds, in any of those messages.... no blanket statement - none intended. =) In my experience, nothing on the web can be contained in a blanket statement *grins*

QUOTE
modules can slow down some rebuild times


and

QUOTE
My experience with that differs


al-m, I didn't mean to raise your defenses, I was just supplying an alternate suggestion.

pax,
almuhajabah
My bad tongue.gif
kadyellebee
If certain MT tags are used in the include, switching to php includes will make rebuilds faster. (the certain MT tags would probably exclude things like the MTBlog* tags or anything else that is based on a not-frequently-changing variable.) I'm fairly sure of this, because otherwise, MT has to rebuild ALL of the pages that the MTInclude is on. If that's individual archives, it could make posting a comment take an excessively long time. On the other hand, if your MT tags are in a single Index Template that is included on the pages, the rebuild is speeded up, and the time to load the page may increase slightly as the page is created on load. The server I'm on makes no difference really between loading a page with PHP on it vs. one without, but it could be just a good host that I have smile.gif

Kristine
tdellaringa
Okay having read all that: I'd really PREFER to use php includes, simply becaue I can't stand all the rebuilding MT makes you do (I'm used to pMachine.)

Anyway, I actually tried that first - I made a php include and linked to it. It didn't work. I must have done it wrong. Here is what I did:

CODE
My index template stuff...blah blah blah

then my include:
<? include('sidebar.php'); ?>


Now this didn't work at all so my syntax must be wrong. Do I not use the PHP style brackets? How do I include that?

On a side note, I also have to do a couple of static pages. Can I simply make plain old PHP pages and link to them - that would work perfectly with PHP includes too - I could just include my sidebar in that page without even worrying about MT there. I just need to make sure the static pages share the sidebar. Am I making sense?

Tom
LisaJill
Yes you can do that.

The pages the include are on - are they .php? Or have you set them up to be parsed as php?

You haven't given much details or a link, so flying blind at the moment, as far as troubleshooting why it mightn't work.... The syntax looks ok to me but I sometimes miss small things (go figure wink.gif ) so I am not the best person to ask...
tdellaringa
No - the pages I wanted the include in (where I am now using the template suggestion as above) are my MT templates. So in my main index template I put the PHP include and nothing happened.

I'm linking my templates to file - my index page template is indexTemplate.htm on myserver - should it be indexTemplate.php? Maybe because the MT template is not a PHP page?
almuhajabah
QUOTE (kadyellebee @ Mar 31 2004, 05:43 PM)
I'm fairly sure of this, because otherwise, MT has to rebuild ALL of the pages that the MTInclude is on.

I'm sorry to keep dragging this thread off-topic but I'm confused by this. I understand that if you update the template module, you then have to rebuild all pages where the module appears. However, if you are merely rebuilding one of the pages where the module is included (e.g., because you added a new entry), doesn't MT simply grab the current contents of the module and insert them in the template as it rebuilds? Why would it rebuild all the other pages where the module is included?
LisaJill
Yes, you should output all pages .php. There are two places to do this - for the archives, you need to go to weblog config - > preferences and change output file extension to php (default is html) - and change the main indexes to .php.

Should work then. =)
LisaJill
al-m, I think MTFastInclude optimizes this, and I have this installed on my pages because I still use MT Includes (for context sensitive stuff, such as my entries).

However, the inbuilt processing, as I understand it - is well, less than optimal, I guess. =) I believe that for every page it has to be included in, it re-reads the module, writes it in, and repeats through this loop. The above plugin caches the module to speed this process up.

Does that make sense? Someone can correct me if I'm wrong. =)
almuhajabah
Stepan says:

QUOTE
However, using these tags generally slows down your rebuild, because every time an MTInclude tag is processed, MovableType reads the file or loads, compiles and builds the template module.


That's different from what I understood Kristine to be saying. It sounds like it rebuilds the template module once for each time that the module is included so that instead of rebuilding the module once and each of the blog pages once, it rebuilds each of the blog pages once and the module as many times as there are blog pages. Obviously, that does slow down the rebuilding process, especially on individual entry archives and especially if you have a large or complex template module. However, what Krisitine seemed to be saying was that it rebuilt every page where the module is included whenever one page with the module is rebuilt. Perhaps I misunderstood her huh.gif

When you save an entry or post a comment, my understanding is that the following pages are rebuilt: all index pages (unless set to never rebuild, or you're using the rebuild type mod) and all archive pages where the entry appears, which would normally be one by-date archive page, one category archive page, and the individual entry archive page (plus, I believe, the pages for the next and previous entries). This is not a very large number of pages, unless you have lots and lots of index pages - in which case, reducing the number of index pages is more likely to help your rebuild speed than removing the template module includes.

That being the case, the only instance where removing template module includes would make a substantial difference in speed is when you're doing a full rebuild. And using FastInclude would seem to resolve the problem there.

In my opinion, the fact that you do need to rebuild when you make changes is a better reason to switch to PHP or SSI than the speed of rebuilds themselves, especially if you use FastInclude. PHP and SSI are slightly faster but the real savings on rebuild time will come elsewhere.
LisaJill
Well it does rebuild the module for every page it builds. It grabs the template, sees the call to the module, builds the module into the template, and completes building the template.

So if you have modules on your individual entry pages (as I do, all my entries are made up of modules for Post Header, Post Body and Post Footer) then it has to build each one of those modules into the template. Slow slow slow. Same if you have that sidebar on every page. Then even on comments it has to build the sidebar in.

This all builds up, and comment times in MT are notoriously slow as it is =/

does that make some sense?
almuhajabah
I don't think that we're actually disagreeing now about what it is that MT does in regard to template modules, which is what initially confused me about Kristine's post, but we seem to have different ideas about how long it takes. You seem to make far more extensive use of template modules than I do so perhaps that accounts for the difference. If you use multiple large and/or complex template modules on each individual entry page, then it will slow down the building of that page. Of course, having any complex code that produces a large page will slow down the rebuild.

As you know, my comment rebuild times are slow, slow, slow. It isn't because of template modules, because I don't use very many of them. It's because I have lots of index pages, large category archive pages (even with just the titles listed), and complex code everywhere. Switching away from template modules could save me a little rebuild time, but reducing the number of index pages, getting rid of category archives, and simplifying my code would save me a lot more.

YMMV
LisaJill
*nods* Our rebuild times are about the same. Plugins, complex code, category pages. Apprently ditching a lot of the MTArchive stuff would help a lot; but it's not a concession that I'm ready to make.

I don't really think that the modules make my code more complex. I've simply pulled out all of the MTEntries information into 3 seperate modules, it would have to be built anyway. Since I use a large number of templates it has saved me a lot of time in reducing redundant code. =)

But, bleh!
almuhajabah
QUOTE (LisaJill @ Mar 31 2004, 07:58 PM)
Apprently ditching a lot of the MTArchive stuff would help a lot; but it's not a concession that I'm ready to make.

This sums up my own problem with rebuild times. tongue.gif
tdellaringa
Getting this back on topic (at least for me) :cough:

I was able to set the category pages to have the php extension - but I don't see where I can to do that for my main index page.

Also, I made a static php page - but then how can I include the MT tags! Arrgh! Can I not include MT tags in my php pages then?

Tom
LisaJill
To change any index, including main, all you do is change the output file name from index.html to index.php (or whatever the name of the file is, change the extension)

does that help?
tdellaringa
Ohhh DUH! LOL. Okay, great that worked perfect for the index. Now - what about my static page - I made a plain old PHP page (its not in MT at all, not a template).. Now, I wanted to use an MT tag in there - but I'm guessing I can't do that.

Example, in my sidebar.php include (that is used on my index template as well as the category) I have this code:

CODE
<MTArchiveList archive_type="Category">
  <li><a href="<$MTArchiveLink$>"><$MTArchiveTitle$></a></li>
</MTArchiveList>


I'm assuming there is no way this will work unless I make this page an MT template right? Otherwise its just a plain PHP page with no reference to what those tags are...

Tom
LisaJill
That's right. It has to be an MT template to process tags (has to parse through the scripts and database, makes sense even if it is a pain wink.gif )

There's no reason that you can't make this an index template and php include it. If it has no recent comments or entries code, you could even set it NOT to rebuild so that it does not affect your rebuild times.

Does that help?
tdellaringa
I kind of understand. So I would do "Create new index template"? Is that right? Set it to not rebuild. Make the extension php. Include my php sidebar in the template. Then it can read the MT tags inside.

It wouldn't have to rebuild to get the correct categories if they change?

Also, how do I actually link to a template page then - I assume I use the output file name? If I name it static.php then I'd link to that?

Tom
LisaJill
Yes to all of the above. =) If you're not going to put php INSIDE of that you could call it .incl - adamrice posted earlier that will help page load times.

All my includes are .php tho (I run php through them anyway) and I haven't noticed any real issues. My page load times are only slow cos of graphics. =)
patski
Can the MTInclude tag use the contents of say the Keywords field to bring in one of a number of modules? I have two modules which I use to display thumbnail images - one each for portrait and landscape. I thought that putting the module name in the Keywords field might do it.

I've tried this:
CODE
<$MTInclude module="<$MTEntryKeywords$>"$>


but it isn't working.
shelley
Unfortunately, you can't nest MT tags inside one another like that.

The Tag Invoke plugin can get around that limitation.
patski
Thanks Shelley, but sorry, I'm being dense. If my modules are called Portrait and Landscape and I put either in the Keywords, is this how the code should look?

CODE
<MTTagInvoke tag_name="MTInclude">
<MTTagAttribute name="module"><MTEntryKeywords></MTTagAttribute>
</MTTagInvoke>
kristine
That looks right - is it working for you?

Kristine
patski
No! It just displays the text in the keywords field (Portrait). The page is
here. Every entry contains the same text in the extended entry field depending on whether the thumbnails are portrait or landscape which is why I thought a module was a better way of doing things.

I've tried using the compare plugin with the MTInclude tag as an alternative but I wanted the greater flexibility to choose one of a number of modules based on the text in the keywords field. I could add enough "if equal" lines as a workaround but it's not as elegant a fix!
kristine
In your source, I see this:
CODE
Portrait</MTTagAttribute>
</MTTagInvoke>

That's a sign that the plugin is not installed or not in the correct place.

smile.gif
Kristine
patski
Spot on! tongue.gif

Thank you so much, it works a treat now. I wrapped it in a Compare tag to cope with entries without keywords while I make the changes.
kristine
Cool deal, glad you got it working!! smile.gif

Kristine
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.