-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1461 from scireum/feature/mko/SIRI-1019
Adds a new Macro which checks whether the given guard string is defined or not
- Loading branch information
Showing
3 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
src/main/java/sirius/pasta/tagliatelle/macros/IfNotDefinedMacro.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Made with all the love in the world | ||
* by scireum in Remshalden, Germany | ||
* | ||
* Copyright by scireum GmbH | ||
* http://www.scireum.de - info@scireum.de | ||
*/ | ||
|
||
package sirius.pasta.tagliatelle.macros; | ||
|
||
import sirius.kernel.commons.Strings; | ||
import sirius.kernel.di.std.Register; | ||
import sirius.kernel.tokenizer.Position; | ||
import sirius.pasta.noodle.Environment; | ||
import sirius.pasta.noodle.compiler.CompilationContext; | ||
import sirius.pasta.noodle.compiler.ir.Node; | ||
import sirius.pasta.noodle.macros.BasicMacro; | ||
import sirius.pasta.tagliatelle.rendering.GlobalRenderContext; | ||
import sirius.pasta.tagliatelle.rendering.LocalRenderContext; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.util.List; | ||
|
||
/** | ||
* Checks whether the given {@linkplain GlobalRenderContext#hasGuard(String) guard } is defined or not. | ||
* | ||
* @see GlobalRenderContext#addGuard(String) | ||
*/ | ||
@Register | ||
public class IfNotDefinedMacro extends BasicMacro { | ||
|
||
@Override | ||
protected Class<?> getType() { | ||
return boolean.class; | ||
} | ||
|
||
@Override | ||
protected void verifyArguments(CompilationContext compilationContext, Position position, List<Class<?>> args) { | ||
if (args.size() != 1 || !CompilationContext.isAssignableTo(args.getFirst(), String.class)) { | ||
throw new IllegalArgumentException("Expected a single String as argument."); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isConstant(CompilationContext context, List<Node> args) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public Object invoke(Environment environment, Object[] args) { | ||
String guard = (String) args[0]; | ||
if (Strings.isFilled(guard) && environment instanceof LocalRenderContext localRenderContext) { | ||
GlobalRenderContext globalRenderContext = localRenderContext.getGlobalContext(); | ||
if (globalRenderContext.hasGuard(guard)) { | ||
return false; | ||
} else { | ||
globalRenderContext.addGuard(guard); | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return """ | ||
Checks whether the given guard is defined or not. If the guard is not defined, true is returned and | ||
the guard is added to a Set of guards to the global context of the template. This means that all | ||
subsequent calls to ifNotDefined with the same guard will return false. This allows to run a | ||
block/code exactly once per template. The behavior is somewhat similar to C/C++ #ifndef. | ||
"""; | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public String getName() { | ||
return "ifNotDefined"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/resources/default/taglib/t/ifNotDefined.html.pasta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<i:arg type="String" name="value" description="Contains a string which is used as a guard clause to determine whether the body should be rendered."/> | ||
|
||
<i:pragma name="description"> | ||
Checks whether the defined value is already specified. If the value is not defined, the body of the tag is rendered. | ||
Similar to C/C++ #ifndef. | ||
</i:pragma> | ||
|
||
<i:if test="ifNotDefined(value)"> | ||
<i:render name="body"/> | ||
</i:if> |