Re: Can I get the name of the template from inside the template?
Mike Kienenberger <[email protected]> Thu, 4 May 2023 06:10:02 -0400
| Newsgroups | gmane.comp.jakarta.velocity.user |
|---|---|
| Message-ID | <CAM1yOjZb_ydrjDT78iQq4pP_N_6VZNXvjB2BiMpYFgUxeXa+LA@mail.gmail.com> |
--0000000000002c9ac505fadb61dd Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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=E2=80=AFPM Christopher Schultz < [email protected]> wrote: > Mike, > > What about calling #parse and other (?) types of includes? Same thing wit= h > macros which call other macros etc. Velocity must already know what=E2=80= =99s going > on. Can=E2=80=99t the template get access to that? > > -chris > > > On May 3, 2023, at 17:30, Mike Kienenberger <[email protected]> wrote: > > > > =EF=BB=BFRather 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 t= he > > template around the same time that you set up the context. > > > > > >> On Wed, May 3, 2023 at 11:42=E2=80=AFAM Christopher Schultz < > >> [email protected]> wrote: > >> > >> All, > >> > >> I'd like too be able to get access to the name of the template current= ly > >> 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 t= he > >> 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 hap= py > >> to hack it a little bit and I already have a custom > >> VelocityLayoutServlet with a custom mergeTemplate method, so ther is a= n > >> 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] > > --0000000000002c9ac505fadb61dd--