简体   繁体   中英

Managing MODX Resources and Elements from code editor

I'm importing my plain HTML/CSS website to MODX. I made all of the Elements static, so I can edit them from VS Code + SFTP plugin. But I still have to use MODX Manager to create or delete new Elements.

Is there a convenient way to manage Resources and Elements not switching to the web browser with MODX Manager opened?

It could be a MODX Extra or VS Code plugin watching /asset/template and automatically creating a MODX Template when a new *.template.tpl file detected.

You can try something like this (untested). Fill in the full path to the modx directory and templates directory and run the script.

<?php
try {
    $modxBasePath = '/full/path/to/modx';
    $templatesPath = '/full/path/to/templates';
    $templatesExtension = 'tpl';

    require_once "$modxBasePath/config.core.php";
    require_once MODX_CORE_PATH.'model/modx/modx.class.php';
    
    $modx = new modX();
    $modx->initialize('mgr');

    if (!is_dir($templatesPath)) {
        throw new Exception("Path $templatesPath is not a directory");
    }

    $files = glob("$templatesPath/*.$templatesExtension");

    foreach ($files as $file) {
        $templateName = basename($file, ".$templatesExtension");

        $template = $modx->getObject('modTemplate', ['templatename' => $templateName]);
        if (empty($template)) {
            $template = $modx->newObject('modTemplate', ['templatename' => $templateName]);
        }

        $template->set('content', file_get_contents($file));

        if (!$template->save()) {
            throw new Exception("Failed to save template $templateName.");
        }
    }

    $cm = $modx->getCacheManager();
    $cm->refresh();
    
} catch (Throwable $ex) {
    die($ex->getMessage());
}

Probably Gitify extra is what you need

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM