Re: [Templates] Can a template tell if it has been PROCESSed or INCLUDEd?

2014-09-25 Thread Kiss Gabor (Bitman)
> This won't work.  That variable will be there in an INCLUDE as well,
> because, "The 'INCLUDE' directive localises (i.e. copies) all variables
> before processing the template."[1]  It doesn't give you a clean slate.

E...
You are right. I don't know how I thought. :-(

Gabor

___
templates mailing list
templates@template-toolkit.org
http://mail.template-toolkit.org/mailman/listinfo/templates


Re: [Templates] Can a template tell if it has been PROCESSed or INCLUDEd?

2014-09-25 Thread Andy Lester

On Sep 25, 2014, at 1:13 PM, Chad Wallace  wrote:

> This won't work.  That variable will be there in an INCLUDE as well,
> because, "The 'INCLUDE' directive localises (i.e. copies) all variables
> before processing the template."[1]  It doesn't give you a clean slate.


Argh, thanks for catching my leap in logic.  You saved me the 10 minutes of 
trying it out.

I'll try Andy's version later.  Thanks, all.

xoa

--
Andy Lester => www.petdance.com

___
templates mailing list
templates@template-toolkit.org
http://mail.template-toolkit.org/mailman/listinfo/templates


Re: [Templates] Can a template tell if it has been PROCESSed or INCLUDEd?

2014-09-25 Thread Chad Wallace
On Thu, 25 Sep 2014 12:35:20 -0500
Andy Lester  wrote:

> However, that does give me an idea.  Perhaps if I look for an
> app-wide global variable that should always exist, and it's not
> there, then I've been INCLUDEd correctly.

This won't work.  That variable will be there in an INCLUDE as well,
because, "The 'INCLUDE' directive localises (i.e. copies) all variables
before processing the template."[1]  It doesn't give you a clean slate.

[1]
http://template-toolkit.org/docs/manual/Directives.html#section_INCLUDE


-- 

C. Chad Wallace, B.Sc.
The Lodging Company
http://www.lodgingcompany.com/
OpenPGP Public Key ID: 0x262208A0


___
templates mailing list
templates@template-toolkit.org
http://mail.template-toolkit.org/mailman/listinfo/templates


Re: [Templates] Can a template tell if it has been PROCESSed or INCLUDEd?

2014-09-25 Thread Andy Wardley

On 25/09/2014 18:35, Andy Lester wrote:

The point of the check is for a template that knows that it must be
invoked via INCLUDE to complain if it's been invoked with PROCESS.


Something else that came to mind while I was out walking the dog...

If you don't mind subclassing Template::Context then you could put your 
own wrapper methods around include() and process() to set a stash variable.


  package Your::Template::Context;
  use base 'Template::Context';

  sub include {
  my ($self, $template, $params) = @_;
  $self->stash->set( tt_call => 'INCLUDE' );
  # don't be tempted to call $self->SUPER::include()
  # because that's a wrapper around process()
  return $self->SUPER::process($template, $params, 1);
  }

  sub process {
  my $self = shift;
  $self->stash->set( tt_call => 'PROCESS' );
  return $self->SUPER::process(@_);
  }

Engage it like so:

  use Template;
  use Your::Template::Context;
  $Template::Config::CONTEXT  = 'Your::Template::Context';

Then [% tt_call %] will be set to 'INCLUDE' or 'PROCESS' depending on 
how the template was called.


Note that the WRAPPER directive calls the include() method (and thus is 
"safe" for your purposes) so tt_call will be set to 'INCLUDE' in that case.


Cheers
Andy


___
templates mailing list
templates@template-toolkit.org
http://mail.template-toolkit.org/mailman/listinfo/templates


Re: [Templates] Can a template tell if it has been PROCESSed or INCLUDEd?

2014-09-25 Thread Andy Lester

On Sep 25, 2014, at 12:17 PM, Kiss Gabor (Bitman)  wrote:

> Just define a global variable in the outermost file:
> [% SET processed = 1 %]
> In the included file you will see this scalar unintialized.


The point of the check is for a template that knows that it must be invoked via 
INCLUDE to complain if it's been invoked with PROCESS.  I can't have it rely on 
being invoked in a certain way by the programmer because I want to check for 
incorrect invocations.

However, that does give me an idea.  Perhaps if I look for an app-wide global 
variable that should always exist, and it's not there, then I've been INCLUDEd 
correctly.

--
Andy Lester => www.petdance.com

___
templates mailing list
templates@template-toolkit.org
http://mail.template-toolkit.org/mailman/listinfo/templates


Re: [Templates] Can a template tell if it has been PROCESSed or INCLUDEd?

2014-09-25 Thread Kiss Gabor (Bitman)
> Any suggestions?

Just define a global variable in the outermost file:
[% SET processed = 1 %]
In the included file you will see this scalar unintialized.

(This version is not perfect because cannot detect INCLUDE+PROCESS
chains. but you can develop is necessary. :-)

Gabor

___
templates mailing list
templates@template-toolkit.org
http://mail.template-toolkit.org/mailman/listinfo/templates


Re: [Templates] Can a template tell if it has been PROCESSed or INCLUDEd?

2014-09-25 Thread Andy Wardley

Hi Andy


Is this possible to do in TT?


Nope, there's nothing I can think of that will allow you to determine 
that from within the template.


One work-around would be to INCLUDE a local BLOCK inside the template.

  [% INCLUDE my_local_block %]
  [% BLOCK my_local_block %]
 
  [% END %]

It's a bit clumsy but it does at least ensure that the template markup 
in the BLOCK will always be protected by an INCLUDE, even if the 
template is loaded by a PROCESS.


Cheers
Andy


___
templates mailing list
templates@template-toolkit.org
http://mail.template-toolkit.org/mailman/listinfo/templates


[Templates] Can a template tell if it has been PROCESSed or INCLUDEd?

2014-09-25 Thread Andy Lester

I have some Template Toolkit templates that must only be invoked with INCLUDE, 
not PROCESS, because they set variables that could have global side effects, 
and INCLUDE localizes the variables before handling the template.
I would like to put some sort of assertion in the template that checks to see 
if it was called with INCLUDE and not PROCESS. In order to do that, I need to 
be able to tell, in the template, how the template was invoked.

Is this possible to do in TT?  I have looked at the "component" variable inside 
the template, and I see no difference between the two if a file is PROCESSed 
vs. INCLUDEd.

Any suggestions?

Thanks,

Andy  


--
Andy Lester => www.petdance.com

___
templates mailing list
templates@template-toolkit.org
http://mail.template-toolkit.org/mailman/listinfo/templates