ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Is there a native EE temporary variable available in templates?

May 16, 2011 1:47pm

Subscribe [5]
  • #1 / May 16, 2011 1:47pm

    johnwbaxter

    651 posts

    Hi,

    I want to assign the output of a channel query to a temp variable so i can use it in a safecracker form. So something like the below:

    {exp:channel:entries channel="staff_info" author_id="17"}
    
    {preload_replace:the_variable="{url_title}"}
    
    {/exp:channel:entries}
    {exp:safecracker channel="staff_info" return="user" author_only="yes" url_title="{the_variable}"}
    {/exp:safecracker}

    I see that preload_replace is run very late so this would not work in reality.

    I don’t want to use php but know i could. Is there a way to do this using some native EE action?

    Thanks!

  • #2 / May 16, 2011 10:15pm

    Sue Crocker

    26054 posts

    Hi, audiopleb. Were you aware that you can set up SafeCracker to use the url_title in the third segment? What are you trying to accomplish?

  • #3 / May 17, 2011 4:47am

    johnwbaxter

    651 posts

    Hey Sue,

    Yes, i am aware that i can use the third segment. Let me tell you what i’m trying to do and see if that helps.

    I’m trying to make a system where users are able to have their own update-able profile which is stored in a single entry in a single channel. The channel only contains one entry for each user which has them assigned as the author of that entry. The users are only allowed to edit their entry in this channel, not add new ones.

    So in the front end, i’m trying to make a SAEF form for the user to update their profile. They do this by clicking a “Edit profile” link that is in the left column of the site. This menu item (site.com/user/edit_profile is the link)is common to all logged in members and so needs to be relevant to any user. Hence why i am not able to use url_title or entry_id to select the entry as it would be different for each user.

    So, what i wanted to do was to use a channel query to select their entry from the user profiles channel by member id, then take the {url_title} from that channel entry to put into the safecracker url_title= parameter.

    I know this sounds a bit convoluted but i can’t see any other way to have the flexible user profile i want in the back, edited from the front.

    So essentially, i want to grab that url_title value from a channel query and use it in a safecracker tag as a parameter. So far i seem to be getting very caught out by the parse order :(

    Any help you can provide would be grand.

    Thanks

    John

  • #4 / May 18, 2011 11:35am

    Sue Crocker

    26054 posts

    Hmmm.. how are these entries created initially?

    I have a few ideas, but want to find that out first.

  • #5 / Jun 06, 2012 4:56pm

    rblitadmin

    7 posts

    I’m aware I’m quite late in posting this, but hopefully it can help someone else out.

    I’ve come across this problem before, and managed to come up with a solution, although my situation was a bit more complicated.  I wanted users to be able to create or edit single-entry items, like you, except each item was to be paired with an already-existing entry.  Basically, I wanted users to be able to enter hotel reservation information for company events, and be able to come back and edit or view their information after it was saved.  A user could have reservation information for any number of company events, but for each event there should only be one reservation entry per user.  My solution involved an outer template embedding an inner template and passing in the {url_title} variable, with the inner template set to execute PHP on input.

    In your solution, though, you only need one entry for each user, period, so you only need one template, set to run PHP on input.  This solution relies on a consistent naming scheme for each profile entry’s URL title.  For example, ‘profile-USER_ID’, where USER_ID is the member ID number of the current user.

    Note: the rather convoluted solution that follows would not be necessary if SafeCracker allowed a failed url_title lookup to degrade gracefully into a ‘create’ form.  If so, you could just use this:

    {exp:safecracker channel="member-profiles" url_title="profile-{member_id}" return="member-profiles/index"}

    If the profile with the given URL title exists, it would become an edit form for the record; if not, it would become a create form.  Unfortunately, if no record with the given URL title exists, SafeCracker shows an error message.

    Here’s my code, altered to reflect your situation:

    <?php
     $profile_url_title = 'profile-' . $this->EE->session->userdata['member_id'];
     
     $profile_exists = $this->EE->db->query('
      SELECT
       url_title
      FROM
       exp_channel_titles
      WHERE
       url_title = "' . $this->EE->db->escape_str($profile_url_title) . '"
      ')->num_rows() != 0;
     
    ?>
    
     <?php if ($profile_exists) { ?>
      {exp:safecracker channel="member-profiles" url_title="<?=$profile_url_title ?>" return="member-profiles/index" include_jquery="no"}
     <?php } else { ?>
      {exp:safecracker channel="member-profiles" return="member-profiles/index" include_jquery="no"}
     <?php } ?>
      
      {exp:member:custom_profile_data}
       <input type='hidden' name='title' id='title' value='Profile for {first_name} {last_name}'></input>
       <input type='hidden' name='url_title' id='url_title' value='profile-{member_id}'></input>
      {/exp:member:custom_profile_data}
    
      // Profile form…
    
     {/exp:safecracker}

    Because the template is set to execute PHP on Input, the results of the database query can be used to alter the SafeCracker form tag appropriately.  Note that you should take care to prevent users from being able to directly view profile information, as simply changing the URL title or entry ID in the address bar would allow them to look at any member’s profile.  Note that the “return=” parameter on the SafeCracker form does not use “ENTRY_ID”, it returns to /index, which I presume would be the template containing the above code; that’s up to the developer, of course.


    If you’d rather shie away from raw database queries, you could use an outer template which would embed the template above.

    Outer template:

    {exp:channel:entries channel="member-profiles" url_title="profile-{member_id}"}
     {if no_results}
      {embed="member-profiles/.profile-form" profile_exists="no"}
     {if:else}
      {embed="member-profiles/.profile-form" profile_exists="yes"}
     {/if}
    {/exp:channel:entries}

    In the inner template (member-profiles/.profile-form), just replace the database query with this:

    $profile_exists = ("{embed:profile_exists}" == "yes");

    The magic that makes this work is the fact that {embed} variables are parsed before Input-PHP is.


    If you’d rather avoid PHP altogether, you could create two embeddable profile form templates, one for creating a record and one for updating.

    Outer template:

    {exp:channel:entries channel="member-profiles" url_title="profile-{member_id}"}
     {if no_results}
      {embed="member-profiles/.create-form"}
     {if:else}
      {embed="member-profiles/.update-form"}
     {/if}
    {/exp:channel:entries}

    Create template: (member-profiles/.create-form)

    {exp:safecracker channel="member-profiles" return="member-profiles/index" include_jquery="no"}
      
      {exp:member:custom_profile_data}
       <input type='hidden' name='title' id='title' value='Profile for {first_name} {last_name}'></input>
       <input type='hidden' name='url_title' id='url_title' value='profile-{member_id}'></input>
      {/exp:member:custom_profile_data}
    
      // Profile form…
    
     {/exp:safecracker}

    Update template: (member-profiles/.update-form)

    {exp:safecracker channel="member-profiles" url_title="profile-{member_id}" return="member-profiles/index" include_jquery="no"}
      
      {exp:member:custom_profile_data}
       <input type='hidden' name='title' id='title' value='Profile for {first_name} {last_name}'></input>
       <input type='hidden' name='url_title' id='url_title' value='profile-{member_id}'></input>
      {/exp:member:custom_profile_data}
    
      // Profile form…
    
     {/exp:safecracker}


    None of the above solutions were actually tested by me, since my situation was different, but they should work.

  • #6 / Jun 07, 2012 3:20am

    granadabip

    38 posts

    putting a very lightly, url_title = “{member_id}-profile” ‘ve tried to put this, “profile-’{member_id}’” forgive me if I screw up I have not actually read the whole post, I simply see that it is best to put quotes around the variable to the interpreter as a string, but not nearly what I’m getting. sorry if I bothered

  • #7 / Jun 07, 2012 5:20am

    Rob Allen

    3114 posts

    It’s easiest to just use the entryID in the URL to select the correct entry.

    Sample edit link, using CURRENT_USER (logged in user) as the author:

    {exp:channel:entries channel="staff_info" author_id="CURRENT_USER" limit="1"}
    <a href="/user/edit_profile/{entry_id}">Edit profile</a>
    {/exp:channel:entries}

    Then in the edit template use a Safecracker form tag like this:

    {exp:safecracker channel="listings" return="/user/edit_profile/ENTRY_ID/updated" error_handling="inline" author_only="yes" entry_id="{segment_3}" preserve_checkboxes="yes"}
  • #8 / Jun 12, 2012 10:22am

    Shane Eckert

    7174 posts

    Hey Rob and all,

    Good conversation here! Nice.

    Going to close this off as it’s a pretty old thread.

    If you need anything else, please just let me know by opening a new thread.

    Cheers,

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases