Skip to content

Commit

Permalink
create() Twig function
Browse files Browse the repository at this point in the history
Resolves #3921
  • Loading branch information
brandonkelly committed Feb 27, 2019
1 parent 4d35e39 commit d7598c8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

### Added
- Added the `create()` Twig function. ([#3921](https://github.com/craftcms/cms/pull/3921))
- Added `craft\helers\Assets::getAllowedFileKinds()`.

### Changed
Expand Down
16 changes: 16 additions & 0 deletions docs/dev/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ Clones a given object.
{% set articles = clone(query).type('articles') %}
```

## `create( type )`

Creates a new object instance based on a given class name or object configuration. See <api:Yii::createObject()> for a full explanation of supported arguments.

```twig
{# Pass in a class name #}
{% set cookie = create('yii\\web\\Cookie') %}
{# Or a full object configuration array #}
{% set cookie = create({
class: 'yii\\web\\cookie',
name: 'foo',
value: 'bar'
}) %}
```

## `csrfInput()`

Returns a hidden CSRF Token input. All sites that have CSRF Protection enabled must include this in each form that submits via POST.
Expand Down
1 change: 1 addition & 0 deletions src/web/twig/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,7 @@ public function getFunctions(): array
new \Twig_SimpleFunction('ceil', 'ceil'),
new \Twig_SimpleFunction('className', 'get_class'),
new \Twig_SimpleFunction('clone', [$this, 'cloneFunction']),
new \Twig_SimpleFunction('create', [Craft::class, 'createObject']),
new \Twig_SimpleFunction('csrfInput', [$this, 'csrfInputFunction']),
new \Twig_SimpleFunction('expression', [$this, 'expressionFunction']),
new \Twig_SimpleFunction('floor', 'floor'),
Expand Down

0 comments on commit d7598c8

Please sign in to comment.