Texen
  1. Texen
  2. TEXEN-5

Allowing putting objects into control context from VTL

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: 1.6
    • Labels:
      None

      Description

      I think it would be nice to allow putting objects into control context in VTL.

      The motivation is straightforward - in some cases you need to put more than one object into context before parsing particular template, so method org.apache.velocity.texen.parse(String inputTemplate, String outputFile, String objectID,Object object) doesn't match your need.

      What do you think ?

        Activity

        Hide
        Henning Schmiedehausen added a comment -

        That patch is actually just a band-aid to texen. The whole thing should either be rewritten or go (IMHO). There are newer solutions (i.e. the one in maven-2) that are much more versatile than the aged texen thing.

        Show
        Henning Schmiedehausen added a comment - That patch is actually just a band-aid to texen. The whole thing should either be rewritten or go (IMHO). There are newer solutions (i.e. the one in maven-2) that are much more versatile than the aged texen thing.
        Hide
        Mojmir Hanes added a comment -

        Your code works, but it is called from Java, eg you define in Java code, which templates you're processing

        I'd like to instantiate in standalone Java application texen generator, fill velocity context with necessary objects and start parsing control template, eg

        Context context = new VelocityContext();
        context.put("a","zz");
        context.put("b","zz");
        context.put("c","zz");
        generator.parse("control.vm",context);

        In control.vm I'm using many $generator.parse(...) calls

        $generator.parse("top.vm", "main.html", "title" ,"Html title")
        $generator.parse("left.vm", "main.html", "param1" ,"Value1")
        $generator.parse("middle.vm", "main.html", "param2" ,"Value2")

        This behaviour (calling templates from control.vm) is used in Apache Torque as well (in fact I've copied this idea from Torque )

        I have one template which is processed two times. I need to process it

        • first time with

        param1 equal to value1 and
        param2 equal to value2

        • second time with

        param1 equal to valueA and
        param2 equal to valueB

        So I need in control.vm something like (for first processing)

        $generator.put("param1", "value1")

        $generator.parse("common-template.vm", "page1.html", "param2" ,"value2")

        and then (for second processing)

        $generator.put("param1", "valueA)

        $generator.parse("common-template.vm", "page2.html", "param2" ,"valueB")

        I can't write here any Java code, because I'm in .vm (velocity template) source, so your solution
        doesn't work.

        If controlContext field was protected, I could extend texen Generator and add method
        for putting object to context myself:

        public void put (String objectID, Object object)

        { controlContext.put(objectID, object); }

        and then I'm fine with

        $generator.put(...)

        in my control.vm, but because it's private (and there is not setter method in texen Generator) I don't have access to context.

        Have you got my point ?

        Show
        Mojmir Hanes added a comment - Your code works, but it is called from Java, eg you define in Java code, which templates you're processing I'd like to instantiate in standalone Java application texen generator, fill velocity context with necessary objects and start parsing control template, eg Context context = new VelocityContext(); context.put("a","zz"); context.put("b","zz"); context.put("c","zz"); generator.parse("control.vm",context); In control.vm I'm using many $generator.parse(...) calls $generator.parse("top.vm", "main.html", "title" ,"Html title") $generator.parse("left.vm", "main.html", "param1" ,"Value1") $generator.parse("middle.vm", "main.html", "param2" ,"Value2") This behaviour (calling templates from control.vm) is used in Apache Torque as well (in fact I've copied this idea from Torque ) I have one template which is processed two times. I need to process it first time with param1 equal to value1 and param2 equal to value2 second time with param1 equal to valueA and param2 equal to valueB So I need in control.vm something like (for first processing) $generator.put("param1", "value1") $generator.parse("common-template.vm", "page1.html", "param2" ,"value2") and then (for second processing) $generator.put("param1", "valueA) $generator.parse("common-template.vm", "page2.html", "param2" ,"valueB") I can't write here any Java code, because I'm in .vm (velocity template) source, so your solution doesn't work. If controlContext field was protected, I could extend texen Generator and add method for putting object to context myself: public void put (String objectID, Object object) { controlContext.put(objectID, object); } and then I'm fine with $generator.put(...) in my control.vm, but because it's private (and there is not setter method in texen Generator) I don't have access to context. Have you got my point ?
        Hide
        Will Glass-Husain added a comment -

        Hi,

        I'm not a big texen user, so, could you explain wouldn't this work? You could subclass Generator and call getContext. Or just do the following in calling code.

        Context context = new VelocityContext();
        context.put("a","zz");
        context.put("b","zz");
        context.put("c","zz");
        generator.parse("template1.vm",context);
        generator.parse("template2.vm",context);
        generator.parse("template3.vm",context);

        Show
        Will Glass-Husain added a comment - Hi, I'm not a big texen user, so, could you explain wouldn't this work? You could subclass Generator and call getContext. Or just do the following in calling code. Context context = new VelocityContext(); context.put("a","zz"); context.put("b","zz"); context.put("c","zz"); generator.parse("template1.vm",context); generator.parse("template2.vm",context); generator.parse("template3.vm",context);
        Hide
        Mojmir Hanes added a comment -

        I'd like to parse (process) various velocity templates from control template (eg I have control.vm file written in VTL and call various templates via $generator.parse(...) from control.vm).

        As far as I know there is no way to put more than one object into context before parsing particular template (in VTL).

        Suppose I have template where I generate text according to 2 VTL variables. I can "pass" only one parameter to that template (eg put it to context)

        I know this can be solved via velocimacro, but I'm wondering then why it's possible to put one object in $generator.parse(...) to context but no more then one. Unfortunatelly field controlContext is private in class org.apache.velocity.texen.Generator so extending Generator class doesn't work.

        Show
        Mojmir Hanes added a comment - I'd like to parse (process) various velocity templates from control template (eg I have control.vm file written in VTL and call various templates via $generator.parse(...) from control.vm). As far as I know there is no way to put more than one object into context before parsing particular template (in VTL). Suppose I have template where I generate text according to 2 VTL variables. I can "pass" only one parameter to that template (eg put it to context) I know this can be solved via velocimacro, but I'm wondering then why it's possible to put one object in $generator.parse(...) to context but no more then one. Unfortunatelly field controlContext is private in class org.apache.velocity.texen.Generator so extending Generator class doesn't work.
        Hide
        Will Glass-Husain added a comment -

        Just trying to figure out, why is this needed?

        (1) You can create your own context and pass it in to the parse (String inputTemplate, String outputFile) method

        or

        (2) You can fill the context with a hashtable.

        fillContextHash (Context context, Hashtable objs)

        (yes, this really should be a Map – it's old code).

        WILL

        Show
        Will Glass-Husain added a comment - Just trying to figure out, why is this needed? (1) You can create your own context and pass it in to the parse (String inputTemplate, String outputFile) method or (2) You can fill the context with a hashtable. fillContextHash (Context context, Hashtable objs) (yes, this really should be a Map – it's old code). WILL

          People

          • Assignee:
            Unassigned
            Reporter:
            Mojmir Hanes
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:

              Development