Hot answers tagged

27

In Craft 3 you can get the env using the getenv() function: {% set env = getenv('ENVIRONMENT') %} {% if env == 'dev' %} hello dev {% else %} hello world {% endif %} Or via the app’s config service: {% set env = craft.app.config.env %}


24

When developing locally on Craft you would most likely have devMode => true set in your config file and set to false for your production config. app/config/general.php return array( 'mydevdomain.dev' => array( 'devMode' => true ), 'mylivedomain.com' => array( 'devMode' => false ) ); So you could test whether craft is in dev mode ...


12

I've had luck using "is defined" before using other logic operators. This should do it: {% if entry.title is defined and entry.title | length == 0 %} {% set entry = craft.entries.section('sectionname').slug(craft.request.segment(2)).first() %} {% endif %}


9

For those coming here using Craft 3, it's now Craft::$app->getConfig()->general->devMode.


7

It's as simple as if ( craft()->config->get( 'devMode' ) ) { //do stuff }


7

You can create a simple plugin with an init() method like so: public function init() { craft()->log->removeRoute('WebLogRoute'); craft()->log->removeRoute('ProfileLogRoute'); } That will prevent the extra logging and profiling information to be output in the browser's console when devMode is enabled.


7

You can set a custom "environment label" variable inside config/general.php: return array( // ALL ENVIRONMENTS '*' => array( ), // LOCAL 'mysite.local' => array( 'env' => 'local' ), // DEV 'dev.mysite.com' => array( 'env' => 'dev' ), // PROD 'www.mysite.com' => array( 'env'...


5

If you want to set entry to a new value if entry.title is undefined or 0 length, try this: {% if !(entry is defined and entry.title is defined and entry.title|length) %} {% set entry = craft.entries.section('sectionname').slug(craft.request.segment(2)).first() %} {% endif %}


5

Looks like it was an issue with character encoding. I copied the {% cache unless craft.config.devMode %} from What are the best practices for using the {% cache %} tag?. When I manually retyped out the conditional it worked properly.


4

For others that might be facing an issue similar to this – we found that our Craft site was gradually getting slower and slower over a period of several days, despite using the {% cache %} tag. When we cleared the template cache, the site would be fast again until the cache grew in size and got slow again. It was our uninformed use of the {% cache %} tag ...


4

This article (http://buildwithcraft.com/help/stuck-tasks) from Brad's comment was indeed the way to address this - and our logs did show this exceeding the php execution time. Thanks Brad!


4

You can't disable the debugging output using Twig/templates. However, Craft will only render the debug data if your document' Content-type header is either "text/html" or "application/xhtml+xml" (source). This means that if you're outputting JSON, you can set the template's Content-type to "application/json" (using Craft's header tag), preventing Craft ...


4

There already was a warning about it, but it didn’t really stick out, I guess. Just gave it the actual Warning style treatment, so hopefully this helps the next guy:


4

Add this to your plugin/controller: if (!craft()->config->get('devMode')) { craft()->templates->getTwig()->addExtension(new \Twig_Extension_Debug()); } Thanks to the all-knowing Brandon Kelly, who posted the solution on Slack.


3

I'll be a bit over-the-top here, but this is like complaining that your air conditioning isn't working as well as you'd hope because of the raging fire coming out of the hood of your car. You should absolutely not be running devMode in any publicly-accessible area — it exposes tons of information about your build, and under some circumstances (like if you ...


3

Dev Mode introduces a lot of extra overhead for many processes. It profiles and logs all MySQL queries to the browser's console. For a detailed description of what Dev Mode does, please check out this page of the docs... What Dev Mode Does Specifically, this line is critical... "Dev Mode is definitely not suited for production environments."


3

Since Craft 2.6 you can do this cleanly with the ?? null-coalescing operator: {% if not (entry.title ?? false) %} ...


3

Making it only available to logged in users and/or admin, would make it impossible to debug the site as an anonymous user would see it. I'd say that's an pretty important feature which I think is wrong to remove just because someone might configure craft wrong. But maybe it'd be possible to use a plugin to turn on/off devMode depending on user permissions?


3

Cleared the log files found at /storage/logs/web.log. Tried to view an inner page on the website which threw a Craft "Internal Server Error". Inside the log, I saw "Twig\Error\SyntaxError: Unknown "dump" function". Googled that message and found the solution. For other newcomers, when you set devMode to true, the dump() function is available in your ...


3

The goal for enabling dev mode so I can troubleshoot things while not showing those ugly errors to anyone else. If that's all you're looking for, there is a per-user account setting for this under the user account's preferences:


2

The current solution i know of is to change CLogFilter's default dumper from: public $dumper='var_export'; To: public $dumper='print_r'; It even has a comment above it: /** * @var callable or function which will be used to dump context information. * Defaults to `var_export`. If you're experiencing issues with circular references * problem change it ...


2

Off the top of my head, I don't think it's going to be possible, at least in the context of outputting it from a Twig variable. That info comes from Yii's CProfileLogRoute class, which Craft extends at etc\logging\ProfileLogRoute. But with all of Yii's logging, that stuff doesn't get flushed to disk (for FileLogRoute) or to the browser's console (for ...


2

Lifting from this answer to a similar question: You'll have to write a simple, custom plugin, and add the following to the init() method in your plugin's primary class: craft()->log->removeRoute('WebLogRoute'); craft()->log->removeRoute('ProfileLogRoute'); For instant success, you can scaffold your plugin by visiting the plugin factory – name ...


2

I finally figured this out! For any iOS application, in order to test the "Production" alerts to a physical iPhone Device -> You have to create an Ad-Hoc Application and deploy that to your iPhone device. In order to do this, you must have an Ad-Hoc Distribution Profile created in the Member Center of "Certificates, Identifiers, and Profiles. See this ...


2

Should I panic or is this normal? Normal. Each of those [trace] and [profile] log entries will have a stack backtrace listed under them showing you exactly what led up to that query, if you're curious. Usually the SHOW FULL COLUMNS FROM query is coming from an ActiveRecord when Yii queries to get metadata for the underlying table it represents.


2

For Craft 2.6, if a hidden redirect input is set, Craft will now follow it first and fallback to the activateAccountSuccessPath if it's not available.


2

I confirmed this was a regression with the Yii 1.1.16/Craft 2.4 update and it's been fixed for the next post 2.4.2670 update.


2

You’re setting the devMode setting to the string 'false', which seems to be evaluated to true. You need to set it to a boolean. '*' => array( 'devMode' => false, ), '.dev' => array( 'devMode' => true, ),


2

I just was pointed to the correct solution (shame on me I did not find it myself): There already is a config for this: https://docs.craftcms.com/v3/config/config-settings.html#disabledplugins To be used like this: 'production' => [ 'disabledPlugins' => ['cloner', 'cp-field-inspect', 'dumper', 'migration-manager'] ], Thanks gtettelaar for ...


1

I cleared all caches and updated file index, and now everything runs smooth. No idea why, but it fixed the problem.


Only top voted, non community-wiki answers of a minimum length are eligible