Re: Can I get the name of the template from inside the template?

Claude Brisson <[email protected]> Thu, 4 May 2023 18:23:22 +0200
Newsgroups gmane.comp.jakarta.velocity.user
Message-ID <[email protected]>
1.7 specific: If you turn the conf property 
template.provide.scope.control to true, the 'ica' (internal context 
adapter) will contain the current template name (and it will work with 
#parse() and alike).

Now you need to get ahold of the ica. You have several ways of doing 
this, for instance:

- use a custom directive
- use an event handler
- populate your context with a Renderable reference


   Claude

On 04/05/2023 12:10, Mike Kienenberger wrote:
> It's been awhile, so you might need to fact-check this post.
>
> If I recall correctly, your context only has information that's been
> provided by you, either directly with "put" or indirectly via helper code
> you add.
>
> While it's true that velocity itself knows all of that information, your
> context is isolated from all of that.    So you could put a reference to
> velocity internals into your template, I suppose.   Or better a simple tool
> that only provides specific information about the current velocity
> internals.
>
> Are you trying to get the name of the main template you are creating?   If
> so, it's really easier to just provide it at the time you specify it in
> your code.
>
> But if you are trying to get the name of an included template or the
> current macro, then you'd need to build some kind of velocity-inspection
> tool.
>
> I seem to recall trying to do something similar many years ago (I only
> cared about the main template name), and I think someone else gave me the
> same advice:  Just put what you are looking for directly into the context
> yourself.   But I think that still holds true for the more advanced case.
>
> On Wed, May 3, 2023 at 7:33 PM Christopher Schultz <
> [email protected]> wrote:
>
>> Mike,
>>
>> What about calling #parse and other (?) types of includes? Same thing with
>> macros which call other macros etc. Velocity must already know what’s going
>> on. Can’t the template get access to that?
>>
>> -chris
>>
>>> On May 3, 2023, at 17:30, Mike Kienenberger <[email protected]> wrote:
>>>
>>> Rather than trying to get the internal name, you'd probably be better
>> off
>>> injecting the name yourself.  You normally have access to the name of the
>>> template around the same time that you set up the context.
>>>
>>>
>>>> On Wed, May 3, 2023 at 11:42 AM Christopher Schultz <
>>>> [email protected]> wrote:
>>>>
>>>> All,
>>>>
>>>> I'd like too be able to get access to the name of the template currently
>>>> being rendered. It looks like that information should be in the
>>>> VelocityContext being used, but I'm having trouble getting a reference
>>>> to that.
>>>>
>>>> This is in a webapp situation, and I'm using velocity-tools so I tried
>>>> the ContextTool which will give me a ChainedContext which implements the
>>>> Context interface but I'm not sure where to go from there. For example,
>>>> I don't see a way to walk my way up the chain to the
>>>> most-parent-context, or really anything that would give me access to
>>>> e.g. getCurrentTemplateName.
>>>>
>>>> Is there a "standard" way to do this?
>>>>
>>>> I'm using Velocity 1.7 at this point (yeah, I know). I'm perfectly happy
>>>> to hack it a little bit and I already have a custom
>>>> VelocityLayoutServlet with a custom mergeTemplate method, so ther is an
>>>> opportunity there to inject some instrumentation.
>>>>
>>>> Thanks,
>>>> -chris
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: [email protected]
>>>> For additional commands, e-mail: [email protected]
>>>>
>>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [email protected]
>> For additional commands, e-mail: [email protected]
>>
>>