2

I have a Craft CMS installation in which there will be other data imported into the db (via Cronjob). It's data from a Weatherstation which will be displayed without further editing in the Control Panel.

Is it possible to display this data via Twig bypassing the Control Panel all-together?

something like:

{% for entry in special.db.section('xyz') %}
  {{ entry.value }}
{% endfor %}

My first thought would be to do this via the Element API Plugin and then use JavaScript to display this data. But maybe that's not necessary?

3

You can create a custom plugin to fetch the data. Pluginfactory.io is a good place to create your basic files/folders. Then you'll insert this function in your PluginVariable.

public function getData($table){
    return craft()->db->createCommand()
        ->select('*')
        ->from($table)
        ->where('some Conditions)
        ->queryAll();
}

and in your template

{% for entry in craft.pluginName.getData('xyz') %}
    {{ entry.someField }}
{% endfor %}

Note: this only works when the table prefix is the same as your craft tables. So before your from($table) Craft will automatically include a $tablePrefix (usually craft_) you can either name your custom tables craft_ as well or change the prefix before your query

$oldPrefix = craft()->db->tablePrefix;
craft()->db->tablePrefix = '';
// handle your queries
craft()->db->tablePrefix = $oldPrefix;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.