Re: Apache Velocity : Verify if all substitutions are made

Debraj Manna <[email protected]> Wed, 14 Jun 2023 21:51:46 +0530
Newsgroups gmane.comp.jakarta.velocity.user
Message-ID <CAF6DVKPHxdq=K0fTE_f=SS0tFfQUMGHjjNhAvVYPeJR7b4jNwA@mail.gmail.com>
--000000000000af667e05fe195aa6
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Strict mode will also suffice for my needs. I am new to Velocity. I am
setting the runtime.reference.strict to true before init() like below

final var engine =3D new VelocityEngine();
engine.setProperty("runtime.references.strict", "true");
engine.init();

I want the Strict mode to be enabled only for some templates. So can this
be overwritten per context or I have to create two VelocityEngine instances
one with strict mode enabled and another without strict mode?

Another follow-up question on the same topic,  is it possible to also check
if all keys specified in VelocityContext are valid for a template. For
example, let's say I have a template, card.vm

card {
    type: CREDIT
    company: VISA
    name: "${firstName} ${lastName}"
  }

If I am then doing something like the below then the above code should
throw an error as cardNumber is not present in the card.vm


VelocityContext context =3D new VelocityContext();
context.put("firstName", "tuk");
context.put("lastName", "man");
*context.put("cardNumber", "1234");*
StringWriter writer =3D new StringWriter();
t.merge( context, writer );


On Wed, Jun 14, 2023 at 11:57=E2=80=AFAM Stefan Gro=C3=9Fhauser <
[email protected]> wrote:

>
> Hello,
>
> isn't the "strict mode" already what you want?
>
>
> https://velocity.apache.org/engine/devel/user-guide.html#strict-rendering=
-mode
>
> best regards
>
> Stefan
>
>
>
> * Von: * Debraj Manna <[email protected]>
> * An: * <[email protected]>
> * Gesendet: * 13.06.2023 11:38
> * Betreff: * Apache Velocity : Verify if all substitutions are made
>
> Can someone let me know how I can verify a velocity context against a
> template? Basically, I want to throw some error if all variables are not
> substituted in the velocity template.
>
> For example, let's say I have a velocity template, card.vm
>
>  card {
>    type: CREDIT
>    company: VISA
>    name: "${firstName} ${lastName}"
>  }
>
> The substitution is done like below
>
> VelocityEngine velocityEngine =3D new VelocityEngine();
> velocityEngine.init();
>
> Template t =3D velocityEngine.getTemplate("card.vm");
>
> VelocityContext context =3D new VelocityContext();
> context.put("firstName", "tuk");
> StringWriter writer =3D new StringWriter();
> t.merge( context, writer );
>
> In this case, I want to throw some error specifying that lastName is not
> replaced in template.
>
> Does velocity provide anything for this?
>
>
>
>
>
>
>
> *JETZT NEWSLETTER ABONNIEREN:
> https://hammerbacher.com/newsletter-anmeldung/
> <https://hammerbacher.com/newsletter-anmeldung/>*
> *Hammerbacher GmbH Registergericht N=C3=BCrnberg HRB 10908*
> *Gesch=C3=A4ftsf=C3=BChrer Bernhard Hammerbacher, Ursula Hammerbacher, Ch=
ristoph
> Hammerbacher, Andreas Hammerbacher*
> Hausanschrift
> Daimlerstra=C3=9Fe 4-6
> D 92318 Neumarkt Telefon
> +49(0)9181
> 2592-0 Telefax
> +49(0)9181
> 2592-28 E-Mail
> [email protected]
> www.hammerbacher.com
>
>
> Haftungsausschluss / Disclaimer
> Die Informationen, die in dieser Kommunikation enthalten sind, sind
> ausschlie=C3=9Flich und allein f=C3=BCr den Empf=C3=A4nger bestimmt. Die =
Verwendung durch
> Dritte ist untersagt. Die Firma Hammerbacher GmbH ist nur f=C3=BCr die vo=
n ihr
> eingegeben Informationen verantwortlich, jedoch nicht f=C3=BCr die einwan=
dfreie
> =C3=9Cbertragung oder im Zusammenhang mit der =C3=9Cbertragung oder dem E=
mpfang
> eingetretene Ver=C3=A4nderungen oder Verz=C3=B6gerungen. Diese E-Mail ent=
h=C3=A4lt
> vetrtrauliche und/oder rechtlich gesch=C3=BCtzte Informationen. Wenn Sie =
nicht
> der richtige Adressat sind oder diese E-Mail irrt=C3=BCmlich erhalten hab=
en,
> informieren Sie bitte sofort den Absender und vernichten Sie diese E-Mail=
.
> Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail ist
> nicht gestattet.
>
>

--000000000000af667e05fe195aa6--