We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38954
    • 24 Posts
    I successfully created template using MODX API (using modTemplate object)

    $template = $modx->newObject('modTemplate');
    $template->set('templatename', $profilename);
    
    if ($template->save()) {
        echo '<p>Template created</p>';
        $templateId = $template->get('id');
    } else {
        die('<p>Template not created.</p>');
    }
    


    The question is how to attach existing template variables to that template.

    Thank you.

    This question has been answered by BobRay. See the first response.

      from Russia with love
      • 4172
      • 5,888 Posts
      you could try to use runProcessor for the whole thing

      $tvs = '[{"id":"25"},{"id":"26"},{"id":"28"}]';
      
      $fields = array(
          'templatename' => $profilename,
          'tvs' => $tvs
       
      );
       
      $response = $modx->runProcessor('template/create', $fields);
       
      if ($response->isError()) {
          $modx->log(modX::LOG_LEVEL_ERROR, 'Failed to create Template');
      }
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 3749
        • 24,544 Posts
        @Bruno17 - A very elegant solution. Do you mind if I borrow it for a blog post (w/ credit to you, of course)?
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 4172
          • 5,888 Posts
          The code initialy was stolen from here:

          http://bobsguides.com/modx-processor-list.html

          So, it is yours, Bob
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 3749
            • 24,544 Posts
            LOL. wink

            This happens to me all the time now. I just had a problem with upgrading XAMPP. After a half-hour of Googling, I found a guy who had the same problem and posted the solution: a link to a page at Bob's Guides. wink
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 38954
              • 24 Posts
              Since MODX gurus are here, I have one more question. I see many people recommend to use runProcessor instead of manipulations with objects. But I was reading somewhere, for the runProcessor to create resource for example, user has to have enough rights, otherwise runProcessor won't work.

              It that right? So for example If I will create a separate php file only with modx core files included to run that in cron, runProcessor will fail?
                from Russia with love
              • discuss.answer
                • 3749
                • 24,544 Posts
                If your cron job executes an external PHP file directly, it's considered to be running in cli mode and MODX will skip all permission checks. If you run by visiting a page that contains a snippet tag, otoh, the permissions will be checked in the processor.

                The advantage of running the processors is that that they will fire the appropriate System Events (e.g., OnDocFormSave).

                If necessary, you can create the template-TV connection directly, which will also bypass the permissions checks:

                $tvId = 12;
                $templateId = 22;
                
                $tvt = $modx->newObject('modTemplateVarTemplate');
                $tvt->set('tmplvarid', $tvId);
                $tvt->set('templateid', $templateId);
                $tvt->set('rank', 3);  // (optional)
                $tvt->save();


                Rank is used only if you want to control the order in which the TVs are displayed in the Manager.
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting