Bug 31786 - Sub template->param($var) should return the value
Summary: Sub template->param($var) should return the value
Status: NEW
Alias: None
Product: Koha
Classification: Unclassified
Component: Templates (show other bugs)
Version: Main
Hardware: All All
: P5 - low enhancement (vote)
Assignee: Bugs List
QA Contact: Testopia
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-10-14 07:25 UTC by Marcel de Rooy
Modified: 2022-10-18 22:36 UTC (History)
4 users (show)

See Also:
Change sponsored?: ---
Patch complexity: ---
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Marcel de Rooy 2022-10-14 07:25:37 UTC
Far easier than 
$template->{VARS}->{singleBranchMode}
would be
$template->param('singleBranchMode')

What do you think?
It seems a simple addition?
Comment 1 Marcel de Rooy 2022-10-14 07:26:47 UTC
Easier or more elegant? Going thru VARS is hacky.
Comment 2 Jonathan Druart 2022-10-17 14:32:28 UTC
What is the context? Why don't you use CGI->param? :)
Comment 3 Marcel de Rooy 2022-10-18 10:56:34 UTC
(In reply to Jonathan Druart from comment #2)
> What is the context? Why don't you use CGI->param? :)

See description too. If earlier code like C4/Auth already pushed info into the template object, it would be nice to read it back later (instead of requerying etc.). Yes, we can access {VARS} but that looks like a bad practice.
In such a case CGI::param might not be in the picture.
Comment 4 David Cook 2022-10-18 22:36:22 UTC
(In reply to Marcel de Rooy from comment #3)
> (In reply to Jonathan Druart from comment #2)
> > What is the context? Why don't you use CGI->param? :)
> 
> See description too. If earlier code like C4/Auth already pushed info into
> the template object, it would be nice to read it back later (instead of
> requerying etc.). Yes, we can access {VARS} but that looks like a bad
> practice.
> In such a case CGI::param might not be in the picture.

In frameworks like Catalyst and Mojolicious, the controller has a "stash" method which provides getting/setting of values to pass to the template. I've certainly used it for both getting and setting in both frameworks.

Catalyst:
$c->stash->{foo} = $bar;
$c->stash( { moose => 'majestic', qux => 0 } );
$c->stash( bar => 1, gorch => 2 ); # equivalent to passing a hashref

Mojolicious:
my $name = $c->stash('name');
$c->stash(text => "Hello $name");

https://metacpan.org/pod/Catalyst#$c-%3Estash
https://docs.mojolicious.org/Mojolicious/Guides/Tutorial#Stash-and-templates
https://mojolicious.io/blog/2017/12/02/day-2-the-stash/

So returning a value from $template->param() does sound reasonable.

However... I thought that we were moving away from $template->param() and to $template->{VARS} in general. (Although I don't see a Coding Guideline that says that.)