0

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.

Max
  • 83
  • 1
  • 7
  • If there isn't such an add-on, do you think creating one makes sence? – Max Nov 25 '22 at 13:07
  • Did not test this, but I believe editing these files directly does not clear the modx cache, where editing the template from within the manager does. This means you will also need to clear the cache directory manually. – Bram Verstraten Nov 28 '22 at 13:13
  • @BramVerstraten, works ok for me, the website updates just after file saving. – Max Nov 29 '22 at 09:59

2 Answers2

2

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());
}
Bram Verstraten
  • 1,414
  • 11
  • 24
0

Probably Gitify extra is what you need

Anton Tarasov
  • 534
  • 1
  • 7
  • 16
  • Nice extra, but it's not what i'm looking for. It allows to upload Elements' content from DB to git, but it doesn't create Elements itself. Add-on which I need creates DB instance for a new _static_ Element when it detects a new static resource on server. – Max Nov 26 '22 at 10:27