9.3: if a variable is not yet existing ?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

9.3: if a variable is not yet existing ?

XL Cordemans
I need to pass a value from one page to another AFTER the user has selected one. But the user can select this on any page he wants. Cookies can be used but the logic should also work if the client blocks the use of cookies. Therefore  I planned to use variables and Session_addVar function for this.  
However, before any selection has been done I need to know if  a value has already been given to that variable, or not. In other words it is possible that there is no variable as there was no selection yet. 
In 9.3 I do not understand yet how to work with a variable if it is not yet existing. I tried to use this:
[if( $Vname->size <= '0'  )]... whatever text ....[/if]
or 
[if( $Vname== ''  )]... whatever value  ....[/if]
However, in both cases it returns an error page saying that the variable Vname does not exist :) . It is however an error page and stops the flow of the rest of the page.
I can find a way around this i, but I was wondering if with lasso 9.3 there is a more direct way? Thanks


#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: 9.3: if a variable is not yet existing ?

stevepiercy
http://www.lassosoft.com/lassoDocs/languageReference/obj/var_defined

Since you don't want to use cookies, that means you need to pass
parameters around in the URL or use forms with buttons.

I have a demo that uses cookies for sessions in a gist.  It also
demonstrates the use of session vars.
https://gist.github.com/stevepiercy/2b6576d2ed8e434ef792

--steve


On 5/23/16 at 4:25 AM, [hidden email] (XL Cordemans) pronounced:

>I need to pass a value from one page to another AFTER the user
>has selected one. But the user can select this on any page he
>wants. Cookies can be used but the logic should also work if
>the client blocks the use of cookies. Therefore  I planned to
>use variables and Session_addVar function for this.  
>However, before any selection has been done I need to know if
> a value has already been given to that variable, or not. In
>other words it is possible that there is no variable as there
>was no selection yet. 
>In 9.3 I do not understand yet how to work with a variable if
>it is not yet existing. I tried to use this:
>[if( $Vname->size <= '0'  )]... whatever text ....[/if]
>or 
>[if( $Vname== ''  )]... whatever value  ....[/if]
>However, in both cases it returns an error page saying that the
>variable Vname does not exist :) . It is however an error page
>and stops the flow of the rest of the page.
>I can find a way around this i, but I was wondering if with
>lasso 9.3 there is a more direct way? Thanks
>
>
>#############################################################
>
>This message is sent to you because you are subscribed to
>the mailing list Lasso [hidden email]
>Official list archives available at http://www.lassotalk.com
>To unsubscribe, E-mail to: <[hidden email]>
>Send administrative queries to  <[hidden email]>

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Steve Piercy              Website Builder              Soquel, CA
<[hidden email]>               <http://www.stevepiercy.com/>


#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: 9.3: if a variable is not yet existing ?

Jolle Carlestam-2
In reply to this post by XL Cordemans
23 maj 2016 kl. 06:25 skrev XL Cordemans <[hidden email]>:
>
> I need to pass a value from one page to another AFTER the user has selected one. But the user can select this on any page he wants. Cookies can be used but the logic should also work if the client blocks the use of cookies. Therefore  I planned to use variables and Session_addVar function for this.  
> However, before any selection has been done I need to know if  a value has already been given to that variable, or not. In other words it is possible that there is no variable as there was no selection yet.
> In 9.3 I do not understand yet how to work with a variable if it is not yet existing. I tried to use this:
> [if( $Vname->size <= '0'  )]... whatever text ....[/if]
> or
> [if( $Vname== ''  )]... whatever value  ....[/if]
> However, in both cases it returns an error page saying that the variable Vname does not exist :) . It is however an error page and stops the flow of the rest of the page.
> I can find a way around this i, but I was wondering if with lasso 9.3 there is a more direct way? Thanks

Steves answer about var_defined is taking care of the direct answer to your question.

However. Using Lasso 9 there are better ways of passing data around than using variables and the built in session manager. I am of the school that you should avoid using any visible variables to handle data. Variables bring along a number of drawbacks. Your problem described here is one of them.

Her are some tips on how to avoid variables.

1/ For data needed within the processed page, or method, use local.
local(mylocal = ’My value’)

2/ If you need to pass data to an included file I have a method, jc_include, that’ll allow you to pass params to the included file. The params are then available as locals in the include.
https://gist.github.com/jolle-c/95847fcd568d63d09aed

Example usage:

jc_include(file('includetest.inc'), -mylocal = #mylocal, -anotherval = ’This works’)

In includetest.inc:

Here is a file that’s included with the values:
<br>
#mylocal
<br>
#anotherval

3/ For when you need to store data between page calls. (Your problem described above) Instead of using the dated and cumbersome built in session handler I have my own; jc_session. You can grab it here:
https://gist.github.com/jolle-c/02b247b2468073608e17

Example usage:
Once the session is created, see the instructions in the gist for that, you add data like this:
jc_session -> data(’a_key', ’My Data’)

To retrieve data:
jc_session -> data('a_key')
This will return the value stored for the key. If there's no matching key it will return void.

Thus you can easily do the test you wanted:
jc_session -> data('Vname’) ? ’Some text’

4/ If you want to create data that is immutable, as in can’t be changed after it’s created. Use a method for that. In the method you do use a variable but it’s hidden from access and tampering.
define company_name => var(__ company_name) || $__ company_name := ’XL Cordemans’

Wherever you want your company name you can now safely use:
company_name

This replaces the need to do:
var(company_name = ’ XL Cordemans’)
var(this = ’that’)
And calls like:
$company_name
$this

Or, worse:
if(var_defined(company_name))
        $company_name
/if

Why is my proposed technique better?
It prevents you from accidentally tamper with the value for company_name.
You can call it wherever you want. Knowing that it will always be there, always have the designated, correct value.
It will not waste processing cycles being defined until called for, thus speeding up time.

The old way of doing this is to have an include file that is used on every call defining maybe hundreds of variables that might be used, and when they are, need to have a correct value. But for any given call maybe only 5% of these variables are actually used. The rest is wasting processing time. And in a complicated project you always suffer the risk that the content of the variable changes unintentionally giving you surprises elsewhere in your code. When that happens it is usually hard to track and correct.

HDB
Jolle

#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: 9.3: if a variable is not yet existing ?

Ke Carlton-3
To expand on this — don't forget setters:

define company_name => var(method_name) || ''
define company_name=(p::string) => var(method_name) := #p

// Setting the company name
company_name = 'The Company'

// Using the company name
'Company: ' + company_name

// Result
Company: The Company

Why is this important? a few reasons...

1. It enforces the type of value set
2. A typo like company_nme = 'value' will error
3. You may want to store the value in a different manner at some point:

define company_name => session->val(method_name) || ''
define company_name=(p::string) => session->val(method_name) := #p

In the real world you're more likely to store references to the company:

// Getter and setter for company id
define current_company_id => session->val(method_name) || 0
define current_company_id=(p::integer) =>  => session->val(method_name) :=
#p

// Cache getter for company —
https://gist.github.com/Ke-/5a631f50eb5366b9cc35
define current_company => cache_method => { return
company(current_company_id) }

// Access the current company name
current_company->name

// Result
'The Company'

Storing the reference to the company is light on sessions / cookies and
means you can use it in an OOP fashion with something like activerow:
https://github.com/Zeroloop/ds/wiki/Active-Row

Ke

On Mon, May 23, 2016 at 6:58 PM Jolle Carlestam <[hidden email]> wrote:

>
>
> 4/ If you want to create data that is immutable, as in can’t be changed
> after it’s created. Use a method for that. In the method you do use a
> variable but it’s hidden from access and tampering.
> define company_name => var(__ company_name) || $__ company_name := ’XL
> Cordemans’
>
>

#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: 9.3: if a variable is not yet existing ?

XL Cordemans
In reply to this post by XL Cordemans
To all, 
thank you for your explanation and to Steve for his very quick reply.

#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>