--- /usr/share/zabbix/include/views/configuration.hostgroups.edit.php.orig	2013-12-12 04:26:07.000000000 +0100
+++ /usr/share/zabbix/include/views/configuration.hostgroups.edit.php	2014-01-06 16:26:05.000000000 +0100
@@ -41,7 +41,7 @@
 	$groupsComboBox->addItem($row['groupid'], $row['name']);
 }
 
-$hostsComboBox = new CTweenBox($hostgroupForm, 'hosts', $this->data['hosts'], 25);
+$hostsComboBox = new CTweenBox($hostgroupForm, 'hosts', $this->data['hosts'], 11);
 foreach ($this->data['db_hosts'] as $host) {
 	// add all hosts except selected
 	if (!isset($this->data['hosts'][$host['hostid']])) {
@@ -56,7 +56,24 @@
 		$hostsComboBox->addItem($host['hostid'], $host['name'], true, false);
 	}
 }
-$hostgroupFormList->addRow(_('Hosts'), $hostsComboBox->get(_('Hosts in'), array(_('Other hosts | Group').SPACE, $groupsComboBox)));
+$hostgroupFormList->addRow(_('Group members'), $hostsComboBox->get(_('Hosts'), array(_('Other hosts | Group').SPACE, $groupsComboBox)));
+
+$templatesComboBox = new CTweenBox($hostgroupForm, 'templates', $this->data['templates'], 11);
+foreach ($this->data['db_templates'] as $template) {
+	// add all templates except selected
+	if (!isset($this->data['templates'][$template['templateid']])) {
+		$templatesComboBox->addItem($template['templateid'], $template['name']);
+	}
+}
+foreach ($this->data['r_templates'] as $template) {
+	if (isset($this->data['rw_templates'][$template['templateid']])) {
+		$templatesComboBox->addItem($template['templateid'], $template['name']);
+	}
+	else {
+		$templatesComboBox->addItem($template['templateid'], $template['name'], true, false);
+	}
+}
+$hostgroupFormList->addRow(null, $templatesComboBox->get(_('Templates'), array(_('Other templates'))));
 
 // append tabs to form
 $hostgroupTab = new CTabView();
--- /usr/share/zabbix/hostgroups.php.orig	2013-10-15 11:25:56.000000000 +0200
+++ /usr/share/zabbix/hostgroups.php	2014-01-06 17:39:13.000000000 +0100
@@ -31,6 +31,7 @@
 // VAR	TYPE	OPTIONAL	FLAGS	VALIDATION	EXCEPTION
 $fields = array(
 	'hosts' =>			array(T_ZBX_INT, O_OPT, P_SYS,	DB_ID,		null),
+	'templates' =>			array(T_ZBX_INT, O_OPT, P_SYS,	DB_ID,		null),
 	'groups' =>			array(T_ZBX_INT, O_OPT, P_SYS,	DB_ID,		null),
 	'hostids' =>		array(T_ZBX_INT, O_OPT, P_SYS,	DB_ID,		null),
 	'groupids' =>		array(T_ZBX_INT, O_OPT, P_SYS,	DB_ID,		null),
@@ -74,6 +75,7 @@
 		'hostids' => $objects,
 		'output' => API_OUTPUT_SHORTEN
 	));
+	$objects = get_request('templates', array());
 	$templates = API::Template()->get(array(
 		'templateids' => $objects,
 		'output' => API_OUTPUT_SHORTEN
@@ -209,6 +211,7 @@
 if (isset($_REQUEST['form'])) {
 	$data['groupid'] = get_request('groupid', 0);
 	$data['hosts'] = get_request('hosts', array());
+	$data['templates'] = get_request('templates', array());
 	$data['name'] = get_request('name', '');
 	$data['twb_groupid'] = get_request('twb_groupid', -1);
 	if ($data['groupid'] > 0) {
@@ -220,12 +223,15 @@
 
 			$options = array(
 				'groupids' => $data['groupid'],
-				'templated_hosts' => 1,
 				'output' => API_OUTPUT_SHORTEN
 			);
 			$data['hosts'] = API::Host()->get($options);
 			$data['hosts'] = zbx_objectValues($data['hosts'], 'hostid');
 			$data['hosts'] = zbx_toHash($data['hosts'], 'hostid');
+
+			$data['templates'] = API::Template()->get($options);
+			$data['templates'] = zbx_objectValues($data['templates'], 'templateid');
+			$data['templates'] = zbx_toHash($data['templates'], 'templateid');
 		}
 	}
 
@@ -246,7 +252,6 @@
 	if ($data['twb_groupid'] == 0) {
 		// get all hosts from all groups
 		$options = array(
-			'templated_hosts' => 1,
 			'sortfield' => 'name',
 			'editable' => 1,
 			'output' => API_OUTPUT_EXTEND
@@ -256,33 +261,46 @@
 		// get hosts from selected twb_groupid combo
 		$options = array(
 			'groupids' => $data['twb_groupid'],
-			'templated_hosts' => 1,
 			'sortfield' => 'name',
 			'editable' => 1,
 			'output' => API_OUTPUT_EXTEND
 		);
 	}
 	$data['db_hosts'] = API::Host()->get($options);
+	$data['db_templates'] = API::Template()->get($options);
 
 	// get selected hosts
 	$options = array(
 		'hostids' => $data['hosts'],
-		'templated_hosts' => 1,
 		'sortfield' => 'name',
 		'output' => API_OUTPUT_EXTEND
 	);
 	$data['r_hosts'] = API::Host()->get($options);
 
+	$options = array(
+		'templateids' => $data['templates'],
+		'sortfield' => 'name',
+		'output' => API_OUTPUT_EXTEND
+	);
+	$data['r_templates'] = API::Template()->get($options);
+
 	// get hosts ids
 	$options = array(
 		'hostids' => $data['hosts'],
-		'templated_hosts' =>1,
 		'editable' => 1,
 		'output' => API_OUTPUT_SHORTEN
 	);
 	$data['rw_hosts'] = API::Host()->get($options);
 	$data['rw_hosts'] = zbx_toHash($data['rw_hosts'], 'hostid');
 
+	$options = array(
+		'templateids' => $data['templates'],
+		'editable' => 1,
+		'output' => API_OUTPUT_SHORTEN
+	);
+	$data['rw_templates'] = API::Template()->get($options);
+	$data['rw_templates'] = zbx_toHash($data['rw_templates'], 'templateid');
+
 	if (!empty($data['groupid'])) {
 		$data['deletableHostGroups'] = getDeletableHostGroups($data['groupid']);
 	}
--- /usr/share/zabbix/include/views/configuration.template.edit.php.orig	2013-10-12 15:51:49.000000000 +0200
+++ /usr/share/zabbix/include/views/configuration.template.edit.php	2014-01-06 18:03:47.000000000 +0100
@@ -87,12 +87,20 @@
 	$hosts_linked_to = API::Host()->get(array(
 		'templateids' => $templateid,
 		'editable' => 1,
-		'templated_hosts' => 1
 	));
 
 	$hosts_linked_to = zbx_objectValues($hosts_linked_to, 'hostid');
 	$hosts_linked_to = zbx_toHash($hosts_linked_to, 'hostid');
 	$templates = $original_templates;
+
+// get template templates from db
+	$templates_linked_to = API::Template()->get(array(
+		'parentTemplateids' => $templateid,
+		'editable' => 1,
+	));
+
+	$templates_linked_to = zbx_objectValues($templates_linked_to, 'hostid');
+	$templates_linked_to = zbx_toHash($templates_linked_to, 'hostid');
 }
 else {
 	$groups = get_request('groups', array());
@@ -100,6 +108,7 @@
 		array_push($groups, $_REQUEST['groupid']);
 	}
 	$hosts_linked_to = get_request('hosts', array());
+	$templates_linked_to = get_request('hosts_templates', array());
 }
 
 $clear_templates = array_intersect($clear_templates, array_keys($original_templates));
@@ -160,7 +169,7 @@
 	$cmbGroups->addItem($group['groupid'], $group['name']);
 }
 
-$host_tb = new CTweenBox($frmHost, 'hosts', $hosts_linked_to, 20);
+$host_tb = new CTweenBox($frmHost, 'hosts', $hosts_linked_to, 10);
 
 // get hosts from selected twb_groupid combo
 $params = array(
@@ -192,11 +201,43 @@
 	$host_tb->addItem($db_host['hostid'], $db_host['name']);
 }
 
-$templateList->addRow(_('Hosts / templates'), $host_tb->Get(_('In'), array(
-	_('Other | group').SPACE,
+$templateList->addRow(_('Linked'), $host_tb->Get(_('In hosts'), array(
+	_('Other hosts | group').SPACE,
 	$cmbGroups
 )));
 
+$template_tb = new CTweenBox($frmHost, 'hosts_templates', $templates_linked_to, 10);
+
+// get templates from selected twb_groupid combo
+$params = array(
+	'groupids' => $twb_groupid,
+	'editable' => 1,
+	'output' => API_OUTPUT_EXTEND
+);
+$db_templates = API::Template()->get($params);
+order_result($db_templates, 'name');
+
+foreach ($db_templates as $hnum => $db_template) {
+	if (isset($templates_linked_to[$db_template['templateid']])) {
+		continue;
+	} // add all except selected templates
+	$template_tb->addItem($db_template['templateid'], $db_template['name']);
+}
+
+// select selected templates and add them
+$params = array(
+	'templateids' => $templates_linked_to,
+	'editable' => 1,
+	'output' => API_OUTPUT_EXTEND
+);
+$db_templates = API::Template()->get($params);
+order_result($db_templates, 'name');
+foreach ($db_templates as $hnum => $db_template) {
+	$template_tb->addItem($db_template['templateid'], $db_template['name']);
+}
+
+$templateList->addRow(null, $template_tb->Get(_('In templates'), _('Other templates')));
+
 // FULL CLONE {
 if ($_REQUEST['form'] == 'full_clone') {
 	// template applications
--- /usr/share/zabbix/templates.php.orig	2013-10-15 11:25:56.000000000 +0200
+++ /usr/share/zabbix/templates.php	2014-01-06 17:58:03.000000000 +0100
@@ -46,6 +46,7 @@
 //		VAR						TYPE		OPTIONAL FLAGS			VALIDATION	EXCEPTION
 $fields = array(
 	'hosts'				=> array(T_ZBX_INT,	O_OPT,	P_SYS,		DB_ID, 		null),
+	'hosts_templates'		=> array(T_ZBX_INT,	O_OPT,	P_SYS,		DB_ID, 		null),
 	'groups'			=> array(T_ZBX_INT, O_OPT,	P_SYS,		DB_ID, 		null),
 	'clear_templates'	=> array(T_ZBX_INT, O_OPT,	P_SYS,		DB_ID, 		null),
 	'templates'			=> array(T_ZBX_STR, O_OPT,	null,		null,		null),
@@ -138,6 +139,7 @@
 elseif (isset($_REQUEST['clone']) && isset($_REQUEST['templateid'])) {
 	unset($_REQUEST['templateid']);
 	unset($_REQUEST['hosts']);
+	unset($_REQUEST['hosts_templates']);
 	$_REQUEST['form'] = 'clone';
 }
 /**
@@ -146,6 +148,7 @@
 elseif (isset($_REQUEST['full_clone']) && isset($_REQUEST['templateid'])) {
 	$_REQUEST['form'] = 'full_clone';
 	$_REQUEST['hosts'] = array();
+	$_REQUEST['hosts_templates'] = array();
 }
 /**
  * Save
@@ -161,6 +164,7 @@
 		$macros = get_request('macros', array());
 		$groups = get_request('groups', array());
 		$hosts = get_request('hosts', array());
+		$hosts_templates = get_request('hosts_templates', array());
 		$templates = get_request('templates', array());
 		$templates_clear = get_request('clear_templates', array());
 		$templateid = get_request('templateid', 0);
@@ -218,6 +222,8 @@
 		$templates_clear = zbx_toObject($templates_clear, 'templateid');
 
 		$hosts = zbx_toObject($hosts, 'hostid');
+		$hosts_templates = zbx_toObject($hosts_templates, 'hostid');
+		$hosts = array_merge($hosts, $hosts_templates);
 
 		$template = array(
 			'host' => $template_name,
