Jump to content
Harmster

"Reset" $config->appendTemplateFile for certain templates

Recommended Posts

Hey,

I am using the Foundation 4 Site Template that Ryan made and I am currently working on a template that is going to be called via ajax and I'd like to just generate the content of the template itself instead of the all the appended/prepended content that the config adds.

How do I "reset" the config properties for just 1 template to the default setting?

Thanks in advance.

Share this post


Link to post
Share on other sites

I'm note sure if it is possible to "reset" the config for a single template.

You can check, if the page is requested via AJAX. In your prepended php, add a if-statement like:

if($config->ajax == false ) {
// Your init.php or code ...
}
//Nothing happens if it is an ajax request.

The file will still be appended but this time no code or markup will be added. The $config->ajax is a PW function which is set to true, if it is an AJAX JS Request.

  • Like 1

Share this post


Link to post
Share on other sites

The thing is that even with this code the template file will still be apended and you'll get the complete html page with <head> <body> etc and I don't need that, I want just 1 html string or a json string without the template files. Sorry if I wasn't clear.

Share this post


Link to post
Share on other sites

Does the "template" that you are calling via ajax really need to be a template file with a PW page that it is attached to?

If you place it in the website root folder and call it from there, PW will leave it alone.

I have done that with files that process ajax form submissions and it should work for your purposes as well.

Share this post


Link to post
Share on other sites

Add 

$useMain = false;

to your template and the html won't output.

  • Like 4

Share this post


Link to post
Share on other sites

If you wanted it to automatically bypass the _main.php template during an ajax request, you could also do something like this in your _init.php:

$useMain = !$config->ajax; 
  • Like 4

Share this post


Link to post
Share on other sites

Hi guys,

I wondering if $useMain is still working. I have my setup like this in the config.php

$config->prependTemplateFile = 'Init.php';

/**
 * appendTemplateFile: PHP file in /site/templates/ that will be loaded after each page's template file
 *
 * Uncomment and edit to enable.
 *
 */
$config->appendTemplateFile = 'Default.php';
 

Then I set the $useMain = false; as suggested but the default is still being output, maybe I am doing something wrong in the Init.php

Thanks guys

Share this post


Link to post
Share on other sites

Just like described above I have a page with a template that is called via ajax. In order to disable $config->appendTemplateFile (named _done.php in my case), I put the following line in _init.php:

$useMain = !$config->ajax; 

However, _done.php still gets called. Is $useMain some kind of special variable? 

Cheers, 

Stefan

Share this post


Link to post
Share on other sites

I found the answer myself. In the Foundation Site Profile, $useMain is initially set to true in _init.php and checked in _main.php:

/templates/_init.php (line 40)

/* * Whether to include the _main.php markup file? 
For example, your template * file would set it to false when generating a page 
for sitemap.xml * or ajax output, in order to prevent display of the main <html> 
document. * */

$useMain = true;

/templates/_main.php (line 14)

// if any templates set $useMain to false, abort displaying this file 
// an example of when you'd want to do this would be XML sitemap or AJAX page. 

if(!$useMain) return;
  • Like 2

Share this post


Link to post
Share on other sites

To whoever might be searching this topic, I just recently found out about 

$config->appendTemplateFile

and started using it in a delayed template method.

In my xml type templates (e.g. rss.php), "exit" seems to have the same effect (bypassing the _main.php template).

if(input()->urlSegment1 != 'rss') throw new Wire404Exception();
	
$rss = wire('modules')->get('MarkupRSS');
$rss->title = 'RSS Feed';
$rss->description = '';
$rss->render($items);

exit;

But for AJAX, i might be inclined to use the suggested solutions above.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...