Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
webbasics
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Taddeüs Kroes
webbasics
Commits
97c7d6c3
Commit
97c7d6c3
authored
12 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Added default value to template expression grammar.
parent
ba551f93
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
template.php
+13
-4
13 additions, 4 deletions
template.php
tests/test_template.php
+7
-0
7 additions, 0 deletions
tests/test_template.php
with
20 additions
and
4 deletions
template.php
+
13
−
4
View file @
97c7d6c3
...
...
@@ -71,10 +71,12 @@ require_once 'node.php';
* curly brackets: *{expression}*. The grammar of all expressions that are
* currently supported can be described as follows:
* <code>
* <expression> : {<nested_exp>}
* | {<nested_exp>?<nested_exp>:<nested_exp>} # Conditional statement
* <expression> : {<exp>}
* <exp> : <nested_exp>
* | <nested_exp>?<nested_exp>:<nested_exp> # Conditional statement
* <nested_exp> : <variable>
* | <function>(<nested_exp>) # Static function call
* | <nested_exp>||<nested_exp> # Default value
* | <function>(<nested_exp>) # Static function call
* | <constant>
* | <html>
* <variable> : $<name> # Regular variable
...
...
@@ -83,7 +85,7 @@ require_once 'node.php';
* <function> : <name> # Global function
* | <name>::<name> # Static class method
* <constant> : An all-caps PHP constant: [A-Z0-9_]+
* <html> : A string without parentheses, curly brackets or semicolons: [^(){}:]*
* <html> : A string without parentheses,
pipes,
curly brackets or semicolons: [^()
|
{}:]*
* <name> : A non-empty variable/method name consisting of [a-zA-Z0-9-_]+
* </code>
*
...
...
@@ -423,6 +425,13 @@ class Template extends Node {
}
elseif
(
preg_match
(
"/^([A-Z0-9_]+)$/"
,
$expression
,
$matches
)
)
{
// <constant>
return
self
::
evaluate_constant
(
$expression
,
$root_level
);
}
elseif
(
(
$split_at
=
strpos
(
$expression
,
'||'
,
1
))
!==
false
)
{
// <nested_exp>||<nested_exp>
try
{
return
self
::
evaluate_expression
(
substr
(
$expression
,
0
,
$split_at
),
$data
,
false
);
}
catch
(
\RuntimeException
$e
)
{
return
self
::
evaluate_expression
(
substr
(
$expression
,
$split_at
+
2
),
$data
,
false
);
}
}
}
...
...
This diff is collapsed.
Click to expand it.
tests/test_template.php
+
7
−
0
View file @
97c7d6c3
...
...
@@ -334,11 +334,18 @@ class TemplateTest extends PHPUnit_Framework_TestCase {
$this
->
assert_evaluates
(
'Bar'
,
'ucfirst(strtolower($FOO))'
);
}
function
test_evaluate_default_value
()
{
$this
->
assert_evaluates
(
'bar'
,
'$foo||fallback'
);
$this
->
assert_evaluates
(
'fallback'
,
'$foo.bar||fallback'
);
$this
->
assert_evaluates
(
''
,
'$foo.bar||'
);
}
/**
* @depends test_evaluate_variable_success
* @depends test_evaluate_no_expression
* @depends test_evaluate_condition_spaces
* @depends test_evaluate_function_success
* @depends test_evaluate_default_value
*/
function
test_evaluate_expression_combined
()
{
$this
->
assert_evaluates
(
'Bar'
,
'$true?ucfirst($foo)'
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment