You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "Ravikanth.L" <ra...@yahoo.com> on 2004/09/17 12:18:37 UTC

Clearing content for dynamically constructed variables - Urgent

Hi All,

 I have a problem in clearing the contents of the
variable which i constructed dynamically. 

i construced the variable with the following syntax

$render.recurse($ctx, '\#set($$name = "$theValue")' )

if my $name is "id", the above syntax should create 
$id variable and the value in $theValue should be the
content of $id variable.

Now the problem is...

I have this construction of variable in a macro which
i will be using recursively for processing the
hierarchy. For the top most level, it is able to
create the dynamic variables and assign the value. But
for all sub level hierarchy items, value of $id is not
getting replaced with proper value. It is still taking
the value of the top level $id. I checked that the
$theValue is not returning null or empty String ("")
for child elements.  I am struck at this point. If
anyone knows the solution for this, kindly let me
know.

With Regards,
Ravikanth.L



		
_______________________________
Do you Yahoo!?
Declare Yourself - Register online to vote today!
http://vote.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Clearing content for dynamically constructed variables - Urgent

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Ravikanth,

> Now i am facing one more problem. Suppose i have 5
> elements in the first level of hierarchy and in the
> macro i have construted variables for them and used
> them for rendering. And now if i get only 3 elements
> for the second iteration, it will construct variables
> newly only for the 3 elements. So, still i be will
> left out with five variables( 3 new and 2 old from
> parent hierarchy). So, it will render the data with
> few old values. I read in the velocity user guide
> that, once the variable is set to the context, there
> is no way to remove it from the context or set it to
> null.
As for removing something from the context, take a look at the thread
starting with
   http://www.mail-archive.com/velocity-user@jakarta.apache.org/msg13203.html

> What may be the better solution for my problem?
Each time you call the macro, you can tell what's set by
$catItem.getElementList(), right?  Then why don't you use that?  Or, if
you have access to the Java code of whatever is in $catItem, I would add
some public methods to let Velocity know what to do, a lot more simply. 
MVC, remember?

Best regards,
-- Shinobu Kawai

-- 
Shinobu Kawai(shinobu.kawai@gmail.com)

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Clearing content for dynamically constructed variables - Urgent

Posted by "Ravikanth.L" <ra...@yahoo.com>.
Hi Shinobu,

 Thanks for your reply. I am also using this render
tool in a standalone application. Actually there was
some problem in my java code. After rectifying that,
it is working if the data is present in the child
iteration. 

Now i am facing one more problem. Suppose i have 5
elements in the first level of hierarchy and in the
macro i have construted variables for them and used
them for rendering. And now if i get only 3 elements
for the second iteration, it will construct variables
newly only for the 3 elements. So, still i be will
left out with five variables( 3 new and 2 old from
parent hierarchy). So, it will render the data with
few old values. I read in the velocity user guide
that, once the variable is set to the context, there
is no way to remove it from the context or set it to
null.

What may be the better solution for my problem?

With Regards,
Ravikanth.L


 



		
__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - Send 10MB messages!
http://promotions.yahoo.com/new_mail 

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Clearing content for dynamically constructed variables - Urgent

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Ravikanth,

> I was using the following code:
<snip />

> I have the hierarchy of data :
<snip />

> I was having these settings in my velocity.properties
<snip />

I simulated the code in my environment, and it worked fine...


> I feel its something problem in my velocity settings.
> I am  using velocity-1.4.jar and
> velocity-tools-view-1.1.jar.
Wait a minute, are you saying that you are doing this via the
VelocityViewServlet?  That makes it even more odder...  The
VelocityViewServlet won't set the Context to $ctx, which means the first
round should fail, too.

If you are using the VelocityViewServlet, could you provide us with the
following information?
- an excerpt of your web.xml related to Velocity
- your toolbox.xml

FYI, I was testing your case using the normal ordinary Velocity.

Best regards,
-- Shinobu Kawai

-- 
Shinobu Kawai(shinobu.kawai@gmail.com)

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Clearing content for dynamically constructed variables - Urgent

Posted by "Ravikanth.L" <ra...@yahoo.com>.
And one more thing was i tried the simple example what

you gave. It was also not working for me. it is
printing $id only. it is not printing any value.

With Regards,
Ravikanth.L


	
		
__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - 100MB free storage!
http://promotions.yahoo.com/new_mail 

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


RE: #parse content from a variable?

Posted by Tim Colson <tc...@cisco.com>.
> > > #parse directive and the (View)RenderTool combined?  
> > Yes, I think so. 
> Picky, picky, picky... ;)
:-) 

> Okay, then how about this:
> 1. Create a ResourceLoader that does something like RenderTool.
> 2. Add it to the ResourceLoader stack.
> This way, anything that's not found in the other loaders will 
> be parsed as VTL.

Yep, I like that idea. :-)

> I think a quick-fix should look something like
> public class TimoResourceLoader extends ResourceLoader

Ok -- Thanks -- I'll give that a try, and have a look at the other resource
loaders for inspiration/understanding.


> > Or maybe there is a way to have *.var trigger the custom 
> resource loader?
> None that I know of.
Ok -- probably not an issue anyway.

> OR, of course, you can make a cool tool that does all this 
> stuff for you.
I know that. :-)

Here is why I don't want to use a tool -- I want this to be "same"
experience for HTML Designer -- always use #parse() keeps it simple. I do
the 'magic' on the backend.

I just want to tell them:
1) There are three external resources available:
  "personalized_thingy.vm" - "custom_thingy.vm" - "default_thingy.vm" 
2) If you want to customize the template, add a file here:
/templates/custom_thingy.vm
3) On the page where you want this component, just add :
   #parse("$thingy")
  NOTE: The user will see their own personalized thingy, or if not exists -
fallback to the custom component, or if that is not provided --> the default
thingy.
  NOTE to Developers: personalized will come from Dynamic DB/COOKIE/FORM,
custom will come from filesystem, and default will come from the
standard_templates.jar. 

Cheers,
Timo



---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: #parse content from a variable?

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Timo,

> > Are you saying that you want something that has the function of the
> > #parse directive and the (View)RenderTool combined?  
> Yes, I think so. 
Picky, picky, picky... ;)

> Parse just asks the loader(s) to go find "thingy.vm", right? 
I guess so.

> Normally, this might be found using a file rsrc or webapp rsrc loader,
> perhaps a third loader in the chain might go looking inside the
> Request/Session/App bags?
Okay, then how about this:
1. Create a ResourceLoader that does something like RenderTool.
2. Add it to the ResourceLoader stack.
This way, anything that's not found in the other loaders will be parsed
as VTL.

I think a quick-fix should look something like
public class TimoResourceLoader extends ResourceLoader
{
public abstract InputStream getResourceStream( String source ) 
    throws ResourceNotFoundException
{
    String template = "$render.recurse(${" + source + "})";
    return new ByteArrayInputStream(template.getBytes());
}
}
# Actually, since the loader doesn't have any reference to the context,
# this is the best I can think of...

> Or maybe there is a way to have *.var trigger the custom resource loader?
None that I know of.

> > Why not just put the contents of "A.THINGY" in a template file and
> > always use #parse?
> Because the template of A.THINGY will be variable, possibly from a database,
> or possibly from a form input (think real-time render).
Picky, picky, picky... ;)

OR, of course, you can make a cool tool that does all this stuff for you.

Best regards,
-- Shinobu Kawai

-- 
Shinobu Kawai(shinobu.kawai@gmail.com)

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


RE: #parse content from a variable?

Posted by Tim Colson <tc...@cisco.com>.
> Are you saying that you want something that has the function of the
> #parse directive and the (View)RenderTool combined?  
Yes, I think so. 

> I mean, how is it going to tell whether it's a
> template file or plain VTL?
Howabout if it had a different extension, or a custom loader?

#parse ("thingy.var")
#parse ("thingy.vm")

Parse just asks the loader(s) to go find "thingy.vm", right? 
Normally, this might be found using a file rsrc or webapp rsrc loader,
perhaps a third loader in the chain might go looking inside the
Request/Session/App bags?

Or maybe there is a way to have *.var trigger the custom resource loader?

 
> Why not just put the contents of "A.THINGY" in a template file and
> always use #parse?
Because the template of A.THINGY will be variable, possibly from a database,
or possibly from a form input (think real-time render).

Timo


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: #parse content from a variable?

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Tim,

> > request.setAttribute("A.THINGY", "#set ($foo=1) $foo $bar $bleck");
> > request.setAttribute("thing_to_inclue", "A.THINGY");
> > In the template:
> > #parse ("$thingy_to_parse")
> 
> Say, and before Nathan jumps in to say, "use the tool --
> $evaluate($thingy)"... let me muddy the waters a bit, in that I'd like to
> keep the template using #parse, because sometimes I'll still have
> $thingy_to_parse="/templates/Foo.vm" i.e. a normal file refernce.
Are you saying that you want something that has the function of the
#parse directive and the (View)RenderTool combined?  I think that's too
much responsibility.  I mean, how is it going to tell whether it's a
template file or plain VTL?

Why not just put the contents of "A.THINGY" in a template file and
always use #parse?

Best regards,
-- Shinobu Kawai

-- 
Shinobu Kawai(shinobu.kawai@gmail.com)

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


RE: #parse content from a variable?

Posted by Tim Colson <tc...@cisco.com>.
> request.setAttribute("A.THINGY", "#set ($foo=1) $foo $bar $bleck");
> request.setAttribute("thing_to_inclue", "A.THINGY");
> In the template:
> #parse ("$thingy_to_parse")

Say, and before Nathan jumps in to say, "use the tool --
$evaluate($thingy)"... let me muddy the waters a bit, in that I'd like to
keep the template using #parse, because sometimes I'll still have
$thingy_to_parse="/templates/Foo.vm" i.e. a normal file refernce.

Tim


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


#parse content from a variable?

Posted by Tim Colson <tc...@cisco.com>.
Hey Gang -
  Just going to throw this out there... see if anybody has a bright idea.

I'd like to dynamically tell #parse what to parse...but have the actual
template to parse come from a variable I set inside a servlet.

Pseudo:

request.setAttribute("A.THINGY", "#set ($foo=1) $foo $bar $bleck");
request.setAttribute("thing_to_inclue", "A.THINGY");
In the template:
#parse ("$thingy_to_parse")

Output would be: 1 bar bleck

Said another way... #parse() loads a template using the resource loaders,
right? So is there a way to get it to load from a request variable?

Cheers,
Timo




---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Clearing content for dynamically constructed variables - Urgent

Posted by "Ravikanth.L" <ra...@yahoo.com>.
Hi Shinobu,

I was using the following code:
#macro( parseItem $catItem )
#foreach( $element in $catItem.getElementList())##
 ##loop through the element list for item and add all 
the elements to context
 #set($name = $!element.getName())##
 #set( $value = $element.getValueList().get(0))
 #set($theValue = $!value.getValue())##
 $render.recurse($ctx,'\#set($$name = $theValue)') 
#if($$name == "id") 
<!-- id value : $id and theValue is : $theValue -->
#end
#end ##end of element list
#end ##end of macro

I have the hierarchy of data :

--- 001 (id)
    --002
       -- 003
When i execute the above macro with the given data for
the first iteration it was printing
<!-- id value : 001 and theValue is : 001 --> 
but for the second level it is printing
<!-- id value : 001 and theValue is : 002 --> 

NOte: the value in $id is still 001 instead of 002.

I was having these settings in my velocity.properties
runtime.log = VelocityTranslator.log

# Default name of the loop counter
# variable reference.
directive.foreach.counter.name = velocityCount

# Default starting value of the loop
# counter variable reference.
directive.foreach.counter.initial.value = 1

velocimacro.library = VM_global_library.vm

file.resource.loader.path = C:/myVelocity/Templates

velocimacro.permissions.allow.inline = true
velocimacro.permissions.allow.inline.to.replace.global
= false
velocimacro.permissions.allow.inline.local.scope =
false  
velocimacro.context.localscope = false

I feel its something problem in my velocity settings.
I am  using velocity-1.4.jar and
velocity-tools-view-1.1.jar. I tried commenting the
last four properites listed above,which are related to
velocimacros, but no luck.

With Regards,
Ravikanth.L


		
__________________________________
Do you Yahoo!?
Yahoo! Mail Address AutoComplete - You start. We finish.
http://promotions.yahoo.com/new_mail 

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Clearing content for dynamically constructed variables - Urgent

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Ravikanth,

> i construced the variable with the following syntax
> 
> $render.recurse($ctx, '\#set($$name = "$theValue")' )


> Now the problem is...
> 
> I have this construction of variable in a macro which
> i will be using recursively for processing the
> hierarchy. For the top most level, it is able to
> create the dynamic variables and assign the value. But
> for all sub level hierarchy items, value of $id is not
> getting replaced with proper value. It is still taking
> the value of the top level $id. I checked that the
> $theValue is not returning null or empty String ("")
> for child elements.  I am struck at this point. If
> anyone knows the solution for this, kindly let me
> know.
That is strange.  Can you provide minimal code that shows what is not
working?  In my environment,
    #set($name = "id")
    #set($theValue = "Hello")
    $render.recurse($ctx, '\#set($$name = "$theValue")')
    $id
    #set($theValue = "World")
    $render.recurse($ctx, '\#set($$name = "$theValue")')
    $id
works perfectly fine, and says Hello World.

Best regards,
-- Shinobu Kawai

-- 
Shinobu Kawai(shinobu.kawai@gmail.com)

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org