Giter Site home page Giter Site logo

nystudio107 / craft-emptycoalesce Goto Github PK

View Code? Open in Web Editor NEW
26.0 26.0 2.0 335 KB

Empty Coalesce adds the ??? operator to Twig that will return the first thing that is defined, not null, and not empty

Home Page: https://nystudio107.com/

License: MIT License

PHP 100.00%
coalesce coalescing craft-plugin craft3 craftcms empty operator twig

craft-emptycoalesce's People

Contributors

khalwat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

jlamb1 seanpm2001

craft-emptycoalesce's Issues

Error Attempting to Install via php craft plugin/install

Describe the bug

Installation of plugin fails for Craft v4 via php craft plugin/install

Copying the shell instructions for the plugin via Craft Control panel > Plugin Store and attempting to install via the console results in an error "requirements cannot be resolved to an installable set of packages"

To reproduce

Steps to reproduce the behaviour:

  1. Copy shell cmd from the plugin page on the Craft Plugin Store
  2. Execute the cmd via the console

Expected behaviour

Plugin should be installed

Error

Root composer.json requires nystudio107/craft-emptycoalesce ^1.0 -> satisfiable by nystudio107/craft-emptycoalesce[1.0.0, ..., 1.0.6]
nystudio107/craft-emptycoalesce[1.0.0, ..., 1.0.6] require craftcms/cms ^3.0.0 -> found craftcms/cms[3.0.0, ..., 3.7.42] but it conflicts with your root composer.json require (4.0.2).

Versions

  • Plugin version: 1.0.6
  • Craft version: 4.0.2

Plugin 1.0.3 with Craft 3.1.15 is broken

Using plugin v 1.0.3. My composer.json:

  "require": {
    "php": ">=7.0.0",
    "craftcms/cms": "3.1.15",
    "vlucas/phpdotenv": "^2.4.0",
    "craftcms/aws-s3": "^1.0",
    "craftcms/redactor": "^2.3.2",
    "verbb/super-table": "^2.1.12",
    "dolphiq/redirect": "^1.0.16",
    "fortrabbit/craft-auto-migrate": "^1.1",
    "nystudio107/craft-emptycoalesce": "^1.0"
  },

Result:

yii\base\ErrorException: Declaration of craft\web\twig\Environment::compileSource(Twig_Source $source) should be compatible with Twig\Environment::compileSource(Twig\Source $source) in /home/vagrant/sites/craft-mbari/vendor/craftcms/cms/src/web/twig/Environment.php:0
Stack trace:
#0 /home/vagrant/sites/craft-mbari/vendor/composer/ClassLoader.php(322): ::Composer\Autoload\includeFile()
#1 /home/vagrant/sites/craft-mbari/vendor/craftcms/cms/src/web/View.php(257): Composer\Autoload\ClassLoader->loadClass()
#2 /home/vagrant/sites/craft-mbari/vendor/craftcms/cms/src/web/View.php(257): ::spl_autoload_call()
#3 /home/vagrant/sites/craft-mbari/vendor/craftcms/cms/src/web/View.php(246): craft\web\View->createTwig()
#4 /home/vagrant/sites/craft-mbari/vendor/craftcms/cms/src/web/View.php(337): craft\web\View->getTwig()
#5 /home/vagrant/sites/craft-mbari/vendor/craftcms/cms/src/web/View.php(384): craft\web\View->renderTemplate()
#6 /home/vagrant/sites/craft-mbari/vendor/craftcms/cms/src/web/Controller.php(161): craft\web\View->renderPageTemplate()
#7 /home/vagrant/sites/craft-mbari/vendor/craftcms/cms/src/controllers/TemplatesController.php(78): craft\controllers\TemplatesController->renderTemplate()
#8 /home/vagrant/sites/craft-mbari/vendor/yiisoft/yii2/base/InlineAction.php(57): craft\controllers\TemplatesController->actionRender()
#9 /home/vagrant/sites/craft-mbari/vendor/yiisoft/yii2/base/InlineAction.php(57): ::call_user_func_array:{/home/vagrant/sites/craft-mbari/vendor/yiisoft/yii2/base/InlineAction.php:57}()
#10 /home/vagrant/sites/craft-mbari/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams()
#11 /home/vagrant/sites/craft-mbari/vendor/craftcms/cms/src/web/Controller.php(109): craft\controllers\TemplatesController->runAction()
#12 /home/vagrant/sites/craft-mbari/vendor/yiisoft/yii2/base/Module.php(528): craft\controllers\TemplatesController->runAction()
#13 /home/vagrant/sites/craft-mbari/vendor/craftcms/cms/src/web/Application.php(297): craft\web\Application->runAction()
#14 /home/vagrant/sites/craft-mbari/vendor/yiisoft/yii2/web/Application.php(103): craft\web\Application->runAction()
#15 /home/vagrant/sites/craft-mbari/vendor/craftcms/cms/src/web/Application.php(286): craft\web\Application->handleRequest()
#16 /home/vagrant/sites/craft-mbari/vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application->handleRequest()
#17 /home/vagrant/sites/craft-mbari/web/index.php(21): craft\web\Application->run()
#18 {main}
  • Removing the plugin fixes the problem.
  • Upgrading to Craft 3.1.18 fixes the problem.

Easy enough for us to fix but though I'd report.

Empty string default returns null.

If my last fallback is an empty string, the empty coalesce returns NULL. Is that the expected behavior?

Example:

{% set nullVal = null %}
{% set empty = '' %}

{{ dump(nullVal ??? empty ??? '') }}
{# NULL #}

{{ dump(nullVal ?? '') }}
{# string(0) "" #}

4.0 Release

Question

Hi Andrew,

when can we expect a release for this plugin for Craft 4? Besides the Beta

Thank you!

Empty strings are output from method calls

I’ve encountered some unexpected behaviour when creating strings inline (i.e. not passing in via a variable).

An odd, example that I would’ve expected to work:

{% set foobar = '' ??? 'expected' %}
{{ dump(foobar) }}{# outputs string(0) "" #}

This becomes problematic when doing something like the following:

{% set foobar = craft.app.request.getQueryParam('empty_param') ??? 'expected' %}
{{ dump(foobar) }}{# outputs string(0) "" #}

It does work if I assign this to a variable first:

{% set empty = craft.app.request.getQueryParam('empty_param') %}
{% set foobar = empty ??? 'expected' %}
{{ dump(foobar) }}{# outputs string(8) "expected" #}

Is this something that can be adjusted to allow chaining without assignments?

Twig Runtime Error when evaluating key values of undefined variables

This is an example from the README.
woof is undefined and 'bark' is printed (as expected).

{% set bar = null %}
{% set foo = '' %}
{% set baz = [] %}
{% set foobar = woof ??? bar ??? foo ??? baz ??? 'bark' %}
{{ foobar }} 
{# 'bark' #}

Here, dog is undefined, but I try to get dog.sound.
I still expected 'bark' to be printed, but instead I get a Twig Runtime Error.

{% set bar = null %}
{% set foo = '' %}
{% set baz = [] %}
{% set foobar = dog.sound ??? bar ??? foo ??? baz ??? 'bark' %}
{{ foobar }} 
{#
  Twig Runtime Error – Twig_Error_Runtime
  Variable "dog" does not exist.
 #}

Is there any way around this error?
If not, I think I'd still need to use longer checks like this, which I was hoping to avoid by using this plugin.

{% set bar = null %}
{% set foo = '' %}
{% set baz = [] %}
{% set foobar = dog is defined and dog.sound and dog.sound|length ? dog.sound : bar ??? foo ??? baz ??? 'bark' %}
{{ foobar }}
{# 'bark' #}

Twig Runtime Error when evaluating key values of undefined variables with Twig filters applied

So I have this piece of code which works fine:

{{ entry.leadTitle ??? title ??? entry.title ??? '! No title set anywhere !' }}

So if entry is not defined it moves on. However, entry.leadTitle is a multiline textfield, so if it's filled in, I'd like to apply the Twig nl2br filter.

{{ entry.leadTitle|nl2br ??? title ??? entry.title ??? '! No title set anywhere !' }}

But then I get the runtime error saying entry does not exist.
For the record, the Twig ?? operator raises the same error unfortunately.

As an update I can tell that the |t filter doesn't throw the issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.