Thread: [htmltmpl] Numeric loops
Brought to you by:
samtregar
From: Dan H. <dan...@re...> - 2007-04-30 00:10:59
|
Hi all Is there a way to loop from a lower integer to an higher one? For example, say an article has a rating of 4. I'd like to loop from 1 to 4 and print a star at each iteration. The only way I can see of doing this is create an array ref in the perl coder and pass that to the template, which seems a lot of pointless effort Regards Dan |
From: Karen <kar...@gm...> - 2007-04-30 00:22:15
|
On 4/29/07, Dan Horne <dan...@re...> wrote: > Is there a way to loop from a lower integer to an higher one? For example, > say an article has a rating of 4. I'd like to loop from 1 to 4 and print a > star at each iteration. The only way I can see of doing this is create an > array ref in the perl coder and pass that to the template, which seems a > lot > of pointless effort I've been trying to think of a way to do that with CSS, since currently I'd like to indent things based on a "level" variable passed through the template (and to complicate things, I'd like to max out at a certain point to avoid right-margin squish, considering that in a couple of cases the threads nest over two hundred levels deep). With (one assumes) a rating system with its own ceiling, though, you could go with img src="blahblah/star<TMPL_VAR name="rating">.gif" or what have you, and just have a set of graphics with star1.gif having one star, star2.gif having two stars, and so forth. |
From: Dan H. <dan...@re...> - 2007-04-30 00:36:05
|
On Sun, 2007-04-29 at 19:22 -0500, Karen wrote: > On 4/29/07, Dan Horne <dan...@re...> wrote: > > Is there a way to loop from a lower integer to an higher one? > For example, > say an article has a rating of 4. I'd like to loop from 1 to 4 > and print a > star at each iteration. The only way I can see of doing this > is create an > array ref in the perl coder and pass that to the template, > which seems a lot > of pointless effort > > > With (one assumes) a rating system with its own ceiling, though, you > could go with img src="blahblah/star<TMPL_VAR name="rating">.gif" or > what have you, and just have a set of graphics with star1.gif having > one star, star2.gif having two stars, and so forth. That would work. It's a shame that there isn't a regular loop construct, as I'd like a solution for all sorts of loop instances (Looping through the pagination indexes generated by Data::Page::Navigation for instance) Dan |
From: Michael P. <mp...@pl...> - 2007-04-30 00:35:15
|
Dan Horne wrote: > Hi all > > Is there a way to loop from a lower integer to an higher one? For example, > say an article has a rating of 4. I'd like to loop from 1 to 4 and print a > star at each iteration. The only way I can see of doing this is create an > array ref in the perl coder and pass that to the template, which seems a lot > of pointless effort If you're so concerned about doing it in your Perl code, then I'd do it with Javascript. Create extra image elements depending on the rating value. -- Michael Peters Developer Plus Three, LP |
From: Mathew R. <mat...@ne...> - 2007-04-30 00:40:16
|
> > Is there a way to loop from a lower integer to an higher one? For > example, > say an article has a rating of 4. I'd like to loop from 1 to 4 and > print a > star at each iteration. The only way I can see of doing this is > create an > array ref in the perl coder and pass that to the template, which > seems a lot > of pointless effort > > > I've been trying to think of a way to do that with CSS, since > currently I'd like to indent things based on a "level" variable passed > through the template (and to complicate things, I'd like to max out at > a certain point to avoid right-margin squish, considering that in a > couple of cases the threads nest over two hundred levels deep). > > With (one assumes) a rating system with its own ceiling, though, you > could go with img src="blahblah/star<TMPL_VAR name="rating">.gif" or > what have you, and just have a set of graphics with star1.gif having > one star, star2.gif having two stars, and so forth. > If you just want to print 4 stars, then just do that. * * * * If you are looking for some indentation background, you could try something like the following on a div: style="background:url('star.gif') right repeat-y;" If you are looking for something like <TMPL_FOR 1..4> then you probably need to re-evaluate why you need it - maybe you need to re-think your original problem. That said, there have been previous discussions on whether the TMPL_xxx syntax should support user-defined extensions, and various people have their own hacks to allow such a feature (which would then allow you to build your TMPL_FOR). What is the problem you are trying to solve? Someone here might have solved it already... Mathew |
From: Dan H. <dan...@re...> - 2007-04-30 00:55:41
|
On Mon, 2007-04-30 at 10:40 +1000, Mathew Robertson wrote: > > > > > Is there a way to loop from a lower integer to an higher > > one? For example, > > say an article has a rating of 4. I'd like to loop from 1 to > > 4 and print a > > star at each iteration. The only way I can see of doing this > > is create an > > array ref in the perl coder and pass that to the template, > > which seems a lot > > of pointless effort > > > > > > I've been trying to think of a way to do that with CSS, since > > currently I'd like to indent things based on a "level" variable > > passed through the template (and to complicate things, I'd like to > > max out at a certain point to avoid right-margin squish, considering > > that in a couple of cases the threads nest over two hundred levels > > deep). > > > > With (one assumes) a rating system with its own ceiling, though, you > > could go with img src="blahblah/star<TMPL_VAR name="rating">.gif" or > > what have you, and just have a set of graphics with star1.gif having > > one star, star2.gif having two stars, and so forth. > > > If you just want to print 4 stars, then just do that. * * * * > > If you are looking for some indentation background, you could try > something like the following on a div: > > style="background:url('star.gif') right repeat-y;" > > If you are looking for something like <TMPL_FOR 1..4> then you > probably need to re-evaluate why you need it - maybe you need to re- > think your original problem. That said, there have been previous > discussions on whether the TMPL_xxx syntax should support user-defined > extensions, and various people have their own hacks to allow such a > feature (which would then allow you to build your TMPL_FOR). Well my problem is that sometimes I want to iterate through a numeric range. If an article is ranked 4 stars then the designer can either iterate through each ranking and display a star or he/she can simply output "4 stars." (one customer likes the latter, another wants the former). Likewise, I use Data::Page::Navigation for paginated data - it's a really nice module, and if it displays 10 indexes at the bottom of the page, say from 5 - 14, it would be nice if I could iterate through them rather than doing it in Perl. It's not a religious decision, it just gives the flexibility to the designers. > What is the problem you are trying to solve? Someone here might have > solved it already... I hope this gives enough info. If I'm doing it wrong, I'm only too pleased to be told if there's a better way. Michael Peters suggested Javascript as one solution, although I'd prefer a solution in H::T if possible. I have looked through the docs, and couldn't see anything obvious, but was hoping that perhaps someone had solved this problem before me via some kind of logic that I hadn't thought of. > > Mathew Dan |
From: Mathew R. <mat...@ne...> - 2007-04-30 02:41:10
|
>> If you just want to print 4 stars, then just do that. * * * * >> >> If you are looking for some indentation background, you could try >> something like the following on a div: >> >> style="background:url('star.gif') right repeat-y;" >> >> If you are looking for something like <TMPL_FOR 1..4> then you >> probably need to re-evaluate why you need it - maybe you need to re- >> think your original problem. That said, there have been previous >> discussions on whether the TMPL_xxx syntax should support user-defined >> extensions, and various people have their own hacks to allow such a >> feature (which would then allow you to build your TMPL_FOR). >> > > Well my problem is that sometimes I want to iterate through a numeric > range. If an article is ranked 4 stars then the designer can either > iterate through each ranking and display a star or he/she can simply > output "4 stars." (one customer likes the latter, another wants the > former). This is a pretty good example where we could consider H::T to be lacking. This type of UI choice is definitely something that is unrelated to business logic, and very much related to UI design - so one could argue that choice it should be implemented in the templates. In this case, I would argue for a case where H::T could be extended so that a TMPL_FOR could be used. [ In fact, had I have came across this same problem, since I use my modified version of H::T, I could have implemented a TMPL_FOR or something similar. ] > Likewise, I use Data::Page::Navigation for paginated data - > it's a really nice module, and if it displays 10 indexes at the bottom > of the page, say from 5 - 14, it would be nice if I could iterate > through them rather than doing it in Perl. It's not a religious > decision, it just gives the flexibility to the designers. > I haven't used Data::Page et al. but have previously created my own code to do similar. AFAICT, are you suggesting using D::P / D::P::N to do something like: my $dp = new D::P(...); $ht->param(pages_in_navigation => $dp->pages_in_navigation(...)); ? Then you would need to do some "prorgamming" in your template to get the values into something printable - which may be not in the spirit of H::T. When I created my pager, I emitted template variables into a global hash which eventually got set into the H::T->param() using a template variable "prefix" so that I could use multiple paginations in a single H::T output. Looking at D::P's API it would appear that some shim code needs to do something similar (ie: something like a H::T::DP ) with an API that looks a bit like: $ht = new H::T(..) $dp = new D::P(...); ... H::T::DP->publish( $ht, $dp, prefix => "pager_"); ... $ht->output(); >> What is the problem you are trying to solve? Someone here might have >> solved it already... >> > > I hope this gives enough info. If I'm doing it wrong, I'm only too > pleased to be told if there's a better way. Michael Peters suggested > Javascript as one solution, although I'd prefer a solution in H::T if > possible. I have looked through the docs, and couldn't see anything > obvious, but was hoping that perhaps someone had solved this problem > before me via some kind of logic that I hadn't thought of. > One problem with a javascript solution is that it makes localisation more complex - still do-able, but easier on the server side as your javascript would have to be aware of whether "4 stars" and "stars 4" is the correct ordering (ie: where "stars 4" would be the in some other langauge where the number comes after the description). [ There are already some Perl modules capable of this type of localisation complexity - implementing it Javascript would be possible, but I'm not sure if it would be worthwhile. ] Mathew |
From: Dan H. <dan...@re...> - 2007-04-30 00:59:48
|
On Sun, 2007-04-29 at 20:34 -0400, Michael Peters wrote: > Dan Horne wrote: > > Hi all > > > > Is there a way to loop from a lower integer to an higher one? For example, > > say an article has a rating of 4. I'd like to loop from 1 to 4 and print a > > star at each iteration. The only way I can see of doing this is create an > > array ref in the perl coder and pass that to the template, which seems a lot > > of pointless effort > > If you're so concerned about doing it in your Perl code, then I'd do it with > Javascript. Create extra image elements depending on the rating value. > Well, I'm not "so" concerned (which to me implies some kind of fervour that I don't think I stated). I was simply asking if there was a way to iterate through integer values that I couldn't see in the POD - simple so the designers could decide what to do with the integer - either iterate from 1 to it, or print it out. Doing it in Perl tends to pre- suppose one solution over the other. The designers don't really have problems with loops - they've used H:T and TT, but I don't think they want to learn Javascript. Thanks Dan |
From: Karen <kar...@gm...> - 2007-04-30 02:12:08
|
On 4/29/07, Dan Horne <dan...@re...> wrote: > Well, I'm not "so" concerned (which to me implies some kind of fervour > that I don't think I stated). I was simply asking if there was a way to > iterate through integer values that I couldn't see in the POD - simple > so the designers could decide what to do with the integer - either > iterate from 1 to it, or print it out. Doing it in Perl tends to pre- > suppose one solution over the other. The designers don't really have > problems with loops - they've used H:T and TT, but I don't think they > want to learn Javascript. Plus, even if you use JS you haven't solved the problem for the people who don't use JS. (Of which I'm sometimes one.) If you do give them a loop, they can at least decide whether to iterate it fully, or to use only the "__last__" value and print it. So there's that. (Not bad for a four- or five-star scale, though it tends to break down in my case, where I've got threads that nest out to a couple hundred levels... there's just no simple solution there.) |
From: Alex T. <al...@ac...> - 2007-04-30 06:44:10
|
my $ratingloop = [ map { {RATING => $_} } (1..$ratingmax) ]; or something like that (untested). The effort seems pointless until the flexibility is used by the template designer to do something special at each rating... or something else no one has thought of yet. I'd go with the loop. TMPL_FOR puts logic in the template, which is what HT is all about preventing. 2c On Mon, 30 Apr 2007 12:11:08 +1200, Dan Horne wrote > Hi all > > Is there a way to loop from a lower integer to an higher one? For > example, say an article has a rating of 4. I'd like to loop from 1 > to 4 and print a star at each iteration. The only way I can see of > doing this is create an array ref in the perl coder and pass that to > the template, which seems a lot of pointless effort > > Regards > > Dan > > ------------------------------------------------------------------------- > This SF.net email is sponsored by DB2 Express > Download DB2 Express C - the FREE version of DB2 express and take > control of your XML. No limits. Just data. Click to get it now. > http://sourceforge.net/powerbar/db2/ > _______________________________________________ > Html-template-users mailing list > Htm...@li... > https://lists.sourceforge.net/lists/listinfo/html-template-users |
From: Arkadiy <ar...@ar...> - 2007-04-30 13:10:40
|
Everybody else displays 5 stars, plus "refuse rating" and "Not Interested" options, and highlight current "popular" ratings and allow user to rank, in half increments. On 4/29/07, Alex Teslik <al...@ac...> wrote: > my $ratingloop = [ map { {RATING => $_} } (1..$ratingmax) ]; > > or something like that (untested). > > The effort seems pointless until the flexibility is used by the template > designer to do something special at each rating... or something else no one > has thought of yet. I'd go with the loop. TMPL_FOR puts logic in the template, > which is what HT is all about preventing. > > 2c > > > On Mon, 30 Apr 2007 12:11:08 +1200, Dan Horne wrote > > Hi all > > > > Is there a way to loop from a lower integer to an higher one? For > > example, say an article has a rating of 4. I'd like to loop from 1 > > to 4 and print a star at each iteration. The only way I can see of > > doing this is create an array ref in the perl coder and pass that to > > the template, which seems a lot of pointless effort > > > > Regards > > > > Dan > > > > ------------------------------------------------------------------------- > > This SF.net email is sponsored by DB2 Express > > Download DB2 Express C - the FREE version of DB2 express and take > > control of your XML. No limits. Just data. Click to get it now. > > http://sourceforge.net/powerbar/db2/ > > _______________________________________________ > > Html-template-users mailing list > > Htm...@li... > > https://lists.sourceforge.net/lists/listinfo/html-template-users > > > ------------------------------------------------------------------------- > This SF.net email is sponsored by DB2 Express > Download DB2 Express C - the FREE version of DB2 express and take > control of your XML. No limits. Just data. Click to get it now. > http://sourceforge.net/powerbar/db2/ > _______________________________________________ > Html-template-users mailing list > Htm...@li... > https://lists.sourceforge.net/lists/listinfo/html-template-users > |
From: Sven N. <sve...@sv...> - 2007-05-02 08:54:49
|
Dan Horne wrote: > Is there a way to loop from a lower integer to an higher one? For example, > say an article has a rating of 4. I'd like to loop from 1 to 4 and print a > star at each iteration. The only way I can see of doing this is create an > array ref in the perl coder and pass that to the template, which seems a lot > of pointless effort In this particular case I'd suggest you create 6 graphics (with 1-6 stars) and use something like <img src="stars<TMPL_VAR NAME="stars">.gif" alt="<TMPL_VAR NAME="stars"> stars"> -Sven |