Gossamer Forum
Home : Products : Gossamer Links : Discussions :

What is the right way to call this Variable on Detail page?

Quote Reply
What is the right way to call this Variable on Detail page?
Within detailed.html page, I have added the following:

<%set keya = 'value for keya'%>
<%set keyb = 'value for keyb'%>
<%set keyc = 'value for keyc'%>
... till...
<%set keyz = 'value for keyz'%>


Suppose a Link (example: link id = 2) has following link properties:

rand1 = r
rand2 = u
rand3 = i
rand4 = y
rand5 = a
rand6 = o
rand7 = w
rand8 = p
..till..
rand26 = z


Now.. Ideally what I want to do is, in detailed.html wherever there is <%var1%>, it should display output for <%keyr%> & for <%var2%> it should display output for <%keyu%> (since value for rand1 = r, & rand2 = u).


I understand I can do this: (& create values for var1 to var26)
<%set var1 = 'key'%>
<%set var1 .= $rand1%>

So now var1 = keyr

<%set var2 = 'key'%>
<%set var2 .= $rand2%>

So now var2 = keyu

...
till..
<%set var26 = 'key'%>
<%set var26 .= $rand26%>




However, when I type in <%var1%> it outputs 'keyr' & not 'value for keyr'. (I even tried <%$var1%>, but no success.. I understand I am not using the right code :( )


So, what would be the right way to display value from the appropriate 'key' variable for desired 'var'. So in the above case:

<%var1%> would output value for 'keyr'
<%var2%> would output value for 'keyu'
<%var3%> would output value for 'keyi'
... till ...
<%var26%> would output value for 'keyz'


Thank you.

Vishal
-------------------------------------------------------
Quote Reply
Re: [VishalT] What is the right way to call this Variable on Detail page? In reply to
Hi,

Is there any reason you are doing all this in the template? It sounds very complicated! (the template parser has to process everything, and then render). It would be much better if you had it in a global I think

get_key_values
Code:
sub {
my $_keys = {
a => "value for keya",
b => "value for keyb",
c => "value for keyc",
d => "value for keyd",
# ...
z => "value for keyz",
};

return $_keys->{$_[0]}
}

Then call with:

Code:
<%get_key_values($rand1)%>

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] What is the right way to call this Variable on Detail page? In reply to
 
Quote:
Is there any reason you are doing all this in the template?
Actually yes. :(

I understand it's a long way, however, Considering how it needs to be displayed here are few things:

1. About 14 different rand values need be displayed on a detailed page & they need to be random for every listing.

2. Position of rand1 .. to .. rand26 will be different according to weekdays. + Value of keya .. to .. keyz will be different for according to the weekday. (also, both rand & key can't be repeated/duplicate, i.e. in display they have to be unique)

4. Along with this, on detailed.html I also need to add 'additional info' as either 'prefix' or 'suffix' for certain 'key' values for certain listings.


Initially, I considered the idea of having actual values in link properties & randomizing it, however since I need to use them multiple times & actually add additional info (based on link ID) in the output.

I did consider a few other methods, however, based on the requirements, idea mentioned on first post seems like the best way.. but that is where I am stuck & need your help..


If it is not impossible, setting up multiple vars as shown in first post, would be very helpful to achieve what I need to get done.

Thank you.

Vishal
-------------------------------------------------------

Last edited by:

VishalT: Mar 10, 2020, 8:01 AM
Quote Reply
Re: [Andy] What is the right way to call this Variable on Detail page? In reply to
For certain cities (example: New York, Dallas, Houston, etc. ) I need to have city name as suffix for keya, keyc & keym.

i.e. for listings under Dallas,
keya outout would ideall be 'value for keya dallas'

& for listings under New York,
keya outout would ideall be 'value for keya New York'

& for listings under Houston,
keya outout would ideall be 'value for keya Houston'

etc..

While for certain listing, I need to have state name as Prefix for certain key values.

I understand I can use conditions like
if rand1 = a, .. 'desired output'
elsif rand1 = b, 'desired output '
etc..

However, using if/elsif it would mean, I would need to use 26 if/elsif for each rand.. :(

---------------------------------
However, if this work: (first post method)
<%set var1 = 'key'%>
<%set var1 .= $rand1%>
<%var1%>

It would save significant parsing & processing too.

Vishal
-------------------------------------------------------

Last edited by:

VishalT: Mar 10, 2020, 12:14 PM
Quote Reply
Re: [VishalT] What is the right way to call this Variable on Detail page? In reply to
Hi,

Code:
<%set var1 = 'key'%>
<%set var1 .= $rand1%>
<%var1%>

This should work fine. What if you add some more debugging? So:

Code:
<%set var1 = 'key'%>
<%set var1 .= $rand1%>
VAR1: <%var1%>
RAND1: <%rand1%>

What do you get?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] What is the right way to call this Variable on Detail page? In reply to
Ideally, what I am trying to do is:

1. Output the value of key that would match the value of rand. So for example, if rand1 = r, it would output results for <%keyr%>. Though this could be done using if/elsif , that would not be ideal.

However, by setting another var, (i.e. var1 for rand1, var2 for rand2, as below, I am able to set var1 = keyr.

<%set var1 = 'key'%>
<%set var1 .= $rand1%>

Would mean, now the output for var1 using <%var1%> = <%keyr%>

----------------------------------

2. For listings where I need to add cities as prefix or suffix, while setting the values for key (in detailed.html), I will modify it as
<%set keyr = 'value for keyr'%>
<%set keyr .= " $City"%>

Example keyr for listing in Dallas:
<%keyr%> = value for keyr Dallas

So final output for var1 for listing in Dallas would be

<%var1%> = value for keyr Dallas.

Vishal
-------------------------------------------------------
Quote Reply
Re: [VishalT] What is the right way to call this Variable on Detail page? In reply to
Hi Andy,

I got it working using your Global & a simple if statement :)..
& yeah you were right, your method seems lot better, especially when compared to the clutter my non-programmer method was...

Now that the solution is at hand, I can't believe I was losing sleep over this.

Thanks again for the help :)

Vishal
-------------------------------------------------------
Quote Reply
Re: [VishalT] What is the right way to call this Variable on Detail page? In reply to
Hi,

Glad to hear it :)

The thing with doing it all in the template, is that it makes it a lot more work for the template parser to do. If you are building 1000 categories, it could add double or triple the build time (the vast majority of build time comes from parsing and printing the pages, so with lots of inline if/else/loops etc, it can slow down)

I always try and do it as either a global, or better yet add it as a function to a plugin file (like ULTRAGlobals). Then its already compiled into the memory, and doesn't need to be passed to the template parser as a sub {} function within a global (again, speeding the process up)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] What is the right way to call this Variable on Detail page? In reply to
Hey Andy,

A question about this global.

Code:
sub {
my $_keys = {
a => "value for keya",
b => "value for keyb",
c => "value for keyc",
d => "value for keyd",
# ...
z => "value for keyz",
};

return $_keys->{$_[0]}
}

Is it possible to use fields like 'City', 'State', etc from link-properties within the global?

i.e.

a => "value for keya <%City%>",
b => "<%State%>value for keyb",

So on the detailed page, global will output along with prefix and/or suffix, when they are set inside the global .


Thanks.

Vishal
-------------------------------------------------------
Quote Reply
Re: [VishalT] What is the right way to call this Variable on Detail page? In reply to
Hi,

You could tweak it like:

Code:
sub {
my ($val,$city,$state) = @_;
my $_keys = {
a => "$city for keya",
b => "value for keyb",
c => "value for keyc",
d => "value for keyd",
# ...
z => "value for keyz",
};

return $_keys->{$val}
}

Then when calling it, you would also need:

Code:
<%global_name($key,$City,$State)%>

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] What is the right way to call this Variable on Detail page? In reply to
That didn't work :(...

Code:
Unable to compile 'get_key_values': Global symbol "$city" requires explicit package name at (eval 43) line 8.



Here is an example of the desired final output :

a => "value for keya",
b => "value for keyb",
c => "value for keyc",

--------------------------------------
Example 1: For a Listing in Miami, Florida

value for keya Miami Florida.


--------------------------------------
Example 2: For a Listing in Dallas, Texas
value for keyb Dallas

--------------------------------------
Example 3: For a Listing in Tulsa, Oklahoma
Tulsa value for keyc

---------------------------------------

Global I am using:

Code:
sub {
my ($val,$City,$State) = @_;
my $_keys = {
a => "$City value for keya",
b => "value for keyb",
c => "value for keyc",
d => "value for keyd",
# ...
z => "value for keyz",
};

return $_keys->{$val}
}

Global Name:
get_key_values


Was initially calling it via:
<%get_key_values($rand1)%>
<%get_key_values($rand2)%>
<%get_key_values($rand3)%>
<%get_key_values($rand4)%>
etc..

Now tried to call it via:
<%get_key_values($rand1,$City,$State)%>


But received an error as above :(

Vishal
-------------------------------------------------------
Quote Reply
Re: [VishalT] What is the right way to call this Variable on Detail page? In reply to
It's telling you $city doesn't exist. What is your full global? Particularly line 8, as that is where the issue is around. The code you shared seems ok

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] What is the right way to call this Variable on Detail page? In reply to
btw.. when I changed $city to $City in global. it stopped showing the error.. however it still does not produce 'City' and 'State' value in the output.

Vishal
-------------------------------------------------------
Quote Reply
Re: [Andy] What is the right way to call this Variable on Detail page? In reply to
You posted it at the same time I was posting previous message.

Vishal
-------------------------------------------------------
Quote Reply
Re: [VishalT] What is the right way to call this Variable on Detail page? In reply to
Hi,

So what is the exact global, and tag you are using? And what value is being passed to it? You only have $City set in the "a => " one, so it won't show up on any others until you add it in ;)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] What is the right way to call this Variable on Detail page? In reply to
Here is the full actual global I am using to test this:

Global name: get_key_values

Code:
sub {
# <%get_key_values($rand1)%>

my ($val,$City,$State) = @_;
my $_keys = {


a => " $City personal injury lawyers",
b => "medical malpractice lawyer",
c => "auto accident lawyer",
d => "car accident lawyer",
e => "value for keye",
f => "value for keyf",
g => "value for keyg",
h => "value for keyh",
i => "value for keyu",
j => "value for keyj",
k => "value for keyk",
l => "value for keyl",
m => "value for keym",
n => "value for keyn",
o => "value for keyo",
p => "value for keyp",
q => "value for keyq",
r => "value for keyr",
s => "value for keys",
t => "value for keyt",
u => "value for keyu",
v => "value for keyv",
w => "value for keyw",
x => "value for keyx",
y => "value for keyy",
z => "value for keyy",
# ...
};

return $_keys->{$val}
}

Calling it via: <%get_key_values($rand1,$City,$State)%>

.

For a situation where rand1 = a.. I am getting below output:

Code:
personal injury lawyersDallas

Vishal
-------------------------------------------------------
Quote Reply
Re: [VishalT] What is the right way to call this Variable on Detail page? In reply to
Hi,

Before:

Code:
my ($val,$City,$State) = @_;

Add in:

Code:
use Data::Dumper;
print $IN->header;
print Dumper(@_);

Then run it again. What do you get?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] What is the right way to call this Variable on Detail page? In reply to
You are right.. the Global code was correct..

it's working

Input: <%get_key_values($rand1,$City,$State)%>
Output: Dallas personal injury lawyers

It took the Dumper code trick of yours to make me realize that I was doing multiple tests & in the flow of thoughts edited only one instance, while was checking the other instance.

btw: Output for dumper code
Code:
$VAR1 = 'b'; $VAR1 = 'a'; $VAR1 = 'b'; $VAR1 = 'u'; $VAR1 = 'd'; $VAR1 = 'w'; $VAR1 = 'k'; $VAR1 = 'h'; $VAR1 = 'g'; $VAR1 = 'b'; $VAR1 = 'i'; $VAR1 = 'p'; $VAR1 = 'k'; $VAR1 = 'w'; $VAR1 = 'c'; $VAR1 = 'b'; $VAR1 = 'b'; $VAR2 = 'Dallas'; $VAR3 = 'Texas'; &#65279;

P.S. I suppose, now it is alright to remove 'use Data::Dumper; print $IN->header; print Dumper(@_);' ?

Thanks for helping me get this done :)

Vishal
-------------------------------------------------------

Last edited by:

VishalT: Mar 18, 2020, 9:46 AM
Quote Reply
Re: [VishalT] What is the right way to call this Variable on Detail page? In reply to
haha cool. Yup, you can get rid of the Data::Dumper bit now. That was just for debugging :)

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!