Correct way to render fields from query in tpl? I'm here again to ask for some help, I'm running a query in a custom module to get contents of a node type, with this:

$node_type = "mynodetype";
  $nids = db_query("SELECT * FROM {node} WHERE type = :type", array(':type' =&gt; $node_type))-&gt;fetchCol();
  $nodes = node_load_multiple($nids);


And returning $nodes to the template, I'm using a foreach for $nodes array and I get something like this when I print_r() an element of $nids array.

stdClass Object ( [vid] =&gt; 12658 [uid] =&gt; 1 [title] =&gt; Node title [log]
=&gt; [status] =&gt; 1 [comment] =&gt; 0 [promote] =&gt; 0 [sticky] =&gt; 0 [nid] =&gt;
12658 [type] =&gt; mynodetype [language] =&gt; es [created] =&gt; 2017
[changed] =&gt; 1502159375 [tnid] =&gt; 0 [translate] =&gt; 0
[revision_timestamp] =&gt; 1502159375 [revision_uid] =&gt; 1 [mp_idmod]
=&gt; Array ( [und] =&gt; Array ( [0] =&gt; Array ( [value] =&gt; 1 ) ) )
[mp_nmod] =&gt; Array ( [und] =&gt; Array ( [0] =&gt; Array ( [value]
=&gt; Myvalue [format] =&gt; [safe_value] =&gt; Myvalue ) ) ) [rdf_mapping]
=&gt; Array ( [rdftype] =&gt; Array ( [0] =&gt; sioc:Item [1] =&gt; foaf:Document )
[title] =&gt; Array ( [predicates] =&gt; Array ( [0] =&gt; dc:title ) ) [created]
=&gt; Array ( [predicates] =&gt; Array ( [0] =&gt; dc:date [1] =&gt; dc:created )
[datatype] =&gt; xsd:dateTime [callback] =&gt; date_iso8601 ) [changed] =&gt;
Array ( [predicates] =&gt; Array ( [0] =&gt; dc:modified ) [datatype] =&gt;
xsd:dateTime [callback] =&gt; date_iso8601 ) [body] =&gt; Array ( [predicates]
=&gt; Array ( [0] =&gt; content:encoded ) ) [uid] =&gt; Array ( [predicates] =&gt;
Array ( [0] =&gt; sioc:has_creator ) [type] =&gt; rel ) [name] =&gt; Array (
[predicates] =&gt; Array ( [0] =&gt; foaf:name ) ) [comment_count] =&gt; Array (
[predicates] =&gt; Array ( [0] =&gt; sioc:num_replies ) [datatype] =&gt;
xsd:integer ) [last_activity] =&gt; Array ( [predicates] =&gt; Array ( [0] =&gt;
sioc:last_activity_date ) [datatype] =&gt; xsd:dateTime [callback] =&gt;
date_iso8601 ) ) [cid] =&gt; 0 [last_comment_timestamp] =&gt; 2017
[last_comment_name] =&gt; [last_comment_uid] =&gt; 1 [comment_count] =&gt; 0
[name] =&gt; admin [picture] =&gt; 0 [data] =&gt; b:0; )


And inside the foreach, to render the fields I do it this way:

&lt;?php print $nid-&gt;mp_idmod[LANGUAGE_NONE][0]['value'];  ?&gt;


My question is, this is the right way to render the fields value of each node content type loaded? Or I should do it other way?
Personally, I would create an Entity View Mode for the content type with the fields arranged and formatted how I want them, then you can simply do:

$node = node_load($nid);
$node_view = node_view($node);
print drupal_render($node_view);


In your case, you need multiple nodes, so:

$views = node_view_multiple($nids, "name of entity view mode");
print drupal_render($views);


Now that you are using an entity view mode, you can give the markup you want in the node--node_type--view_mode.tpl.php file, and it will use that.

As for the query and dynamic code, it should go in a preprocess hook, and not the tpl itself. You can return it as a $variable to the template being rendered, usually this is a page callback.

Again, I don't know much about your module or where this code is. You could enable Entity View Modes and just shift this code and get rid of it, and leverage Views instead for the same exact result.