【问题标题】:Managing MODX Resources and Elements from code editor从代码编辑器管理 MODX 资源和元素
【发布时间】:2022-11-28 22:00:21
【问题描述】:

我正在将纯 HTML/CSS 网站导入 MODX。我将所有元素设为静态,因此我可以从 VS Code + SFTP 插件编辑它们。但我仍然必须使用 MODX 管理器来创建或删除新元素。

有没有一种方便的方法来管理资源和元素,而不是在打开 MODX 管理器的情况下切换到 Web 浏览器?

它可能是一个 MODX Extra 或 VS Code 插件,监视 /asset/template 并在检测到新的 *.template.tpl 文件时自动创建一个 MODX 模板。

【问题讨论】:

  • 如果没有这样的附加组件,您认为创建一个有意义吗?
  • 没有对此进行测试,但我相信直接编辑这些文件不会清除 modx 缓存,而在管理器中编辑模板会清除缓存。这意味着您还需要手动清除缓存目录。

标签: visual-studio-code content-management-system development-environment modx


【解决方案1】:

可能 Gitify extra 是你需要的

【讨论】:

  • 不错的额外功能,但这不是我要找的。它允许将 Elements 的内容从 DB 上传到 git,但它不会创建 Elements 本身。我需要的附加组件为新的创建数据库实例静止的当它检测到服务器上的新静态资源时的元素。
【解决方案2】:

你可以尝试这样的事情(未经测试)。 填写 modx 目录和模板目录的完整路径并运行脚本。

<?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());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 2017-08-17
    相关资源
    最近更新 更多