Simple sharedInclude template plugin
authorCyperghost <olaf_schmitz_1@t-online.de>
Fri, 26 Jan 2024 12:43:20 +0000 (13:43 +0100)
committerCyperghost <olaf_schmitz_1@t-online.de>
Fri, 26 Jan 2024 12:43:20 +0000 (13:43 +0100)
12 files changed:
com.woltlab.wcf/templates/__multifactorTotpDevicesContainer.tpl
com.woltlab.wcf/templates/__multifactorTotpNewDeviceContainer.tpl
com.woltlab.wcf/templates/shared_formContainer.tpl
com.woltlab.wcf/templates/shared_formField.tpl
com.woltlab.wcf/templates/shared_rowFormContainer.tpl
com.woltlab.wcf/templates/shared_rowFormFieldContainer.tpl
com.woltlab.wcf/templates/shared_tabFormContainer.tpl
com.woltlab.wcf/templates/shared_tabMenuFormContainer.tpl
com.woltlab.wcf/templates/shared_tabTabMenuFormContainer.tpl
wcfsetup/install/files/lib/system/template/SharedTemplateEngine.class.php
wcfsetup/install/files/lib/system/template/TemplateEngine.class.php
wcfsetup/install/files/lib/system/template/plugin/SharedIncludeFunctionTemplatePlugin.class.php [new file with mode: 0644]

index 26a8a38b23e0a01256f653e7a209117e5a891146..9a6fcd4e076efaaf1d9a81bd82fe80991c449449 100644 (file)
@@ -36,7 +36,7 @@
        </div>
 </section>
 
-{include file='__formContainerDependencies'}
+{sharedInclude file='formContainerDependencies'}
 
 <script data-relocate="true">
        require(['WoltLabSuite/Core/Form/Builder/Field/Dependency/Container/Default'], function(DefaultContainerDependency) {
index 1b3b03f63cce4244335c7134ac1ccae679e8acdf..8b43a2b4f6dbaac2275a8f95eea44c999c3ca274 100644 (file)
@@ -37,7 +37,7 @@
        </div>
 </section>
 
-{include file='__formContainerDependencies'}
+{sharedInclude file='formContainerDependencies'}
 
 <script data-relocate="true">
        require(['WoltLabSuite/Core/Form/Builder/Field/Dependency/Container/Default'], function(DefaultContainerDependency) {
index 6eec208c1a228e09412f5c76f4e1e9245659ef90..a30657efd3ba6312079209b5b3dd0ee73bf5ed8f 100644 (file)
                        <h2 class="sectionTitle">{@$container->getLabel()}{if $container->markAsRequired()} <span class="formFieldRequired">*</span>{/if}</h2>
                {/if}
        {/if}
-       
-       {include file='__formContainerChildren'}
+
+       {sharedInclude file='formContainerChildren'}
 </section>
 
-{include file='__formContainerDependencies'}
+{sharedInclude file='formContainerDependencies'}
 
 <script data-relocate="true">
        require(['WoltLabSuite/Core/Form/Builder/Field/Dependency/Container/Default'], function(DefaultContainerDependency) {
index 9a3f40c80a9f99779d5622aecb55a73cab953cf1..53ed2e78d2cf1b60b9eb102a3f6e1954007b050b 100644 (file)
@@ -7,9 +7,9 @@
        <dd>
                {@$field->getFieldHtml()}
 
-               {include file='shared_formFieldErrors'}
-               {include file='shared_formFieldDescription'}
-               {include file='shared_formFieldDependencies'}
-               {include file='shared_formFieldDataHandler'}
+               {sharedInclude file='formFieldErrors'}
+               {sharedInclude file='formFieldDescription'}
+               {sharedInclude file='formFieldDependencies'}
+               {sharedInclude file='formFieldDataHandler'}
        </dd>
 </dl>
index 28ad88f4b28fb76bb6effedd6d8bb62e5c16dd2c..76b820f72c53850f1dd7826aac1a31104efda9cf 100644 (file)
@@ -3,10 +3,10 @@
        *}{foreach from=$container->getAttributes() key='attributeName' item='attributeValue'} {$attributeName}="{$attributeValue}"{/foreach}{*
        *}{if !$container->checkDependencies()} style="display: none;"{/if}{*
 *}>
-       {include file='__formContainerChildren'}
+       {include file='shared_formContainerChildren'}
 </div>
 
-{include file='__formContainerDependencies'}
+{include file='shared_formContainerDependencies'}
 
 <script data-relocate="true">
        require(['WoltLabSuite/Core/Form/Builder/Field/Dependency/Container/Default'], function(DefaultContainerDependency) {
index 35146da0898f5f17e57fc581f27ad76238b4c1b9..d07f58292a1c22ec70284e8b1aa09fe8867a055f 100644 (file)
@@ -24,7 +24,7 @@
        </dd>
 </dl>
 
-{include file='__formContainerDependencies'}
+{include file='shared_formContainerDependencies'}
 
 <script data-relocate="true">
        require(['WoltLabSuite/Core/Form/Builder/Field/Dependency/Container/Default'], function(DefaultContainerDependency) {
index 13eb804e548232262e2964b09a4e42d91e22b49f..99e0cc6ceefb65e7fc038e1cd1a55448640d21de 100644 (file)
@@ -3,10 +3,10 @@
        *}{foreach from=$container->getAttributes() key='attributeName' item='attributeValue'} {$attributeName}="{$attributeValue}"{/foreach}{*
        *}{if !$container->checkDependencies()} style="display: none;"{/if}{*
 *}>
-       {include file='__formContainerChildren'}
+       {include file='shared_formContainerChildren'}
 </div>
 
-{include file='__formContainerDependencies'}
+{include file='shared_formContainerDependencies'}
 
 <script data-relocate="true">
        require(['WoltLabSuite/Core/Form/Builder/Field/Dependency/Container/Tab'], function(TabContainerDependency) {
index 35c6302f97ee1c28bc765d1f14b1824f88735e63..0aa4039c0b7f7fbc63a4611445ab70b41da92a18 100644 (file)
                        {/foreach}
                </ul>
        </nav>
-       
-       {include file='__formContainerChildren'}
+
+       {include file='shared_formContainerChildren'}
 </div>
 
-{include file='__formContainerDependencies'}
+{include file='shared_formContainerDependencies'}
 
 <script data-relocate="true">
        require(['WoltLabSuite/Core/Form/Builder/Field/Dependency/Container/TabMenu'], function(TabMenuContainerDependency) {
index 44862b771c7aa894909e19978e868f3720f0128c..7d60e23ec301af6ab19936ed53c514ed07c26265 100644 (file)
@@ -11,8 +11,8 @@
                        {/foreach}
                </ul>
        </nav>
-       
-       {include file='__formContainerChildren'}
+
+       {include file='shared_formContainerChildren'}
 </div>
 
-{include file='__formContainerDependencies'}
+{include file='shared_formContainerDependencies'}
index 9ae2a12412d34930aadfebc4359216234bb0ae6a..8f85a8497348231c1d39901ed29972322c6b45b0 100644 (file)
@@ -10,8 +10,9 @@ use wcf\system\WCF;
  * @author  Olaf Braun
  * @copyright   2001-2014 WoltLab GmbH
  * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since 6.1
  */
-class SharedTemplateEngine extends TemplateEngine
+final class SharedTemplateEngine extends TemplateEngine
 {
     /**
      * @inheritDoc
index 87a775d9beeee736b5cd9fbc14f31d0ccb1c470f..60e14c7173880160034be511039c71fa64e7204d 100755 (executable)
@@ -354,6 +354,10 @@ class TemplateEngine extends SingletonFactory
      */
     public function getSourceFilename($templateName, $application)
     {
+        if (\str_starts_with($templateName, 'shared_') && !($this instanceof SharedTemplateEngine)) {
+            throw new \LogicException('Shared templates can only be used with SharedTemplateEngine');
+        }
+
         $sourceFilename = $this->getPath($this->templatePaths[$application], $templateName);
         if (!empty($sourceFilename)) {
             return $sourceFilename;
diff --git a/wcfsetup/install/files/lib/system/template/plugin/SharedIncludeFunctionTemplatePlugin.class.php b/wcfsetup/install/files/lib/system/template/plugin/SharedIncludeFunctionTemplatePlugin.class.php
new file mode 100644 (file)
index 0000000..720eb6e
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+
+namespace wcf\system\template\plugin;
+
+use wcf\system\template\SharedTemplateEngine;
+use wcf\system\template\TemplateEngine;
+
+/**
+ * Usage:
+ *   {sharedInclude file="test" application="wcf"}
+ *   {sharedInclude file="test" application="wcf" var=$variable …}
+ *
+ * @author  Olaf Braun
+ * @copyright   2001-2024 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since 6.1
+ */
+final class SharedIncludeFunctionTemplatePlugin implements IFunctionTemplatePlugin
+{
+    #[\Override]
+    public function execute($tagArgs, TemplateEngine $tplObj)
+    {
+        if (!isset($tagArgs['file'])) {
+            throw new \InvalidArgumentException("missing 'file' argument in sharedInclude tag");
+        }
+        $file = 'shared_' . $tagArgs['file'];
+        $application = $tagArgs['application'] ?? 'wcf';
+        $sandbox = $tagArgs['sandbox'] ?? false;
+
+        unset($tagArgs['file'], $tagArgs['application'], $tagArgs['sandbox']);
+
+        return SharedTemplateEngine::getInstance()->fetch($file, $application, $tagArgs, $sandbox);
+    }
+}