We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1634
    • 51 Posts
    I want to create a calculated template variable using EVAL. I’m going to use this for a ditto sort. My EVAL needs to look at field (a), see if it has a value and return that value if it does. If it doesn’t it looks at field b and returns that value instead. I want to place my EVAL in the ’Default’ field of the tv. The user will never edit this value.

    If I enter the following in the default field:
    @EVAL return 'test';


    Then I see this in the tv when editing a document:
    @EVAL return 'test';


    When I expect to see this:
    test


    We’re running 0.9.6.2

    Thank you in advance modx chums
      Hmmmmm.

      It's all a delusion
      • 22303 MODX Staff
      • 10,725 Posts
      @EVAL is calculated at runtime, i.e. when the page is rendered in the front-end.  It does not get executed when editing a document.
        • 1634
        • 51 Posts
        Ah ha,

        Well that would certainly explain it. Thanks as ever opengeek wink
          Hmmmmm.

          It's all a delusion
          • 630
          • 39 Posts
          Just shout if I’m wrong but what OpenGeek said goes for the content of a tv only. When you use @EVAL in the input options of a tv, for example, it is evaluated when the backend is rendered.
            • 7231
            • 4,205 Posts
            Quote from: agilero at Dec 18, 2008, 02:20 PM

            Just shout if I’m wrong but what OpenGeek said goes for the content of a tv only. When you use @EVAL in the input options of a tv, for example, it is evaluated when the backend is rendered.
            Correct, you can render input in certain TV types with EVAL binding. For example populate a drop down menu with chunk names.
              [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

              Something is happening here, but you don't know what it is.
              Do you, Mr. Jones? - [bob dylan]
              • 74
              • 74 Posts
              @EVAL is calculated at runtime, i.e. when the page is rendered in the front-end. It does not get executed when editing a document
              OpenGeek, thanks for explanation!
              Bad news for me.
              I spended 3days for nothing((
              I need to execute some snippet from TV after backend edit.
              Any possibilities to do this?
                • 9207 ☆ A M B ☆
                • 2,475 Posts
                If you need to run PHP code after an edit, you probably need to write a Plugin. Snippets are triggered when a page loads (on the front-end), whereas a Plugin is triggered by a system event (such as editing a page).
                  • 74
                  • 74 Posts
                  Thanks, Everett!
                  Plugin - new word for me wink
                  I tried  to set parent`s Pagetitle into TV of cildren docs automatically:
                  TV "city"
                  @EVAL $params['tvname'] = 'parent'; return $modx->runSnippet('parentTV', $params);

                  Snippet "parentTV"
                  $params['tvname'] = 'parent'; 
                  //get parent`s Pagetitle
                  $parent = $modx->getParent($id=-1,1,pagetitle);
                  $value = ($parent[pagetitle]);
                  $docID = ($modx->documentIdentifier);
                  //---write TV values to DB
                  if (!function_exists('setTemplateVar')) {
                    function setTemplateVar($value, $docID, $tplVarName) {
                      global $modx;
                      //-- get tmplvar id
                  $tplName = $modx->getFullTableName('site_tmplvars');
                  $tplRS = $modx->db->select('id', $tplName, 'name="' . $tplVarName . '"');
                  $tplRow = $modx->db->getRow($tplRS);
                  
                      $tblName = $modx->getFullTableName('site_tmplvar_contentvalues');
                      $selectQuery = $modx->db->select("*", $tblName, 'contentid=' . $docID . ' AND tmplvarid=' . $tplRow);
                      $updFields = array ( 'value' => $value );
                      $insFields = array (
                      'tmplvarid' => $tplRow,
                      'contentid' => $docID,
                      'value' => $value );
                      if ($modx->db->getRecordCount($selectQuery) < 1) {
                        $modx->db->insert($insFields, $tblName);
                      } else { 
                      $modx->db->update($updFields, $tblName, 'contentid=' . $docID . ' AND tmplvarid=' . $tplRow);
                      }
                    }
                  }
                  @ setTemplateVar($value, $docID, $tplVarName);
                  

                  It writes TV value into DB.
                  But it`s happening only after opening page with this TV from frontend - damn @EVAL!

                  And now..
                  i have no idea how to connect Plugin and TV together.
                    • 9207 ☆ A M B ☆
                    • 2,475 Posts
                    When exactly do you want your script to be run? Do you want this value calculated when a manager edits a page?

                    If I understand you correctly, you have 2 fields in your templates, let’s say a and b. Your plugin could read the values, then choose which one to output. The event that would trigger its execution would be when someone edits a document... I’m not sure exactly which one to use, but login to the control panel --> Resources --> Manage Resources --> Plugin, then choose to create a new Plugin and look at its "System Events" tab. There you get a list of all system events. The following caught my eye under the "Documents" section:

                    • OnDocFormPrerender
                    • OnDocFormRender

                    Here’s one example of how to read a document’s page title:
                    $doc = $modx->getDocument(1);
                    echo $doc[‘pagetitle’];


                    You can probably use the document object api to put values back into a document before it renders. Does that make sense.

                    There isn’t much documentation on Plugins sad but here’s the official page:
                    http://modxcms.com/plugin-examples.html

                    You might learn more by looking at the Document Object: http://modxcms.com/the-document-object.html

                    The one thing I’m not seeing is how to access template variables using the document api... I know how to get them using a sql query, but that’s sort of a pain.

                    Hope this gets you started.
                      • 9207 ☆ A M B ☆
                      • 2,475 Posts
                      Ok, I found the template variable stuff. You can read template variables using the getTemplateVar method:
                      http://modxcms.com/gettemplatevar.html
                      http://modxcms.com/gettemplatevars.html